using AibisDream.Kit; using AibisDream.Utility; using TMPro; using UnityEngine; using UnityEngine.UI; namespace AibisDream { public class ChapterPanel : MonoBehaviour, IUIPanel { #region 索引 public ChapterController chapterController; public Animator animator; public RectTransform chapterContentRect; public TMP_Text errorInfo; public GameObject chapterDetail; public Image chapterDetailImage; public TMP_Text chapterDetailText; public Button continueButton; public Button restartButton; public GameObject chapterItemPrefab; #endregion public Color backgroundColor = new(10.0f / 255.0f, 10.0f / 255.0f, 10.0f / 255.0f, 0.6f); public float delayTime = 0.5f; private GameObject _background; private ChapterVo _curChapterVo; public void Show() { gameObject.SetActive(true); AddBackground(); animator.Play("Open", 0, 0); ActionKit.Delay(delayTime, DrawContent).Start(this); } public void Hide() { RemoveBackground(); animator.Play("Close", 0, 0); ActionKit.Delay(delayTime, DestroyContent).Start(this); } private void AddBackground() { var bgTex = new Texture2D(1, 1); bgTex.SetPixel(0, 0, backgroundColor); bgTex.Apply(); _background = new GameObject("PopupBackground"); var image = _background.AddComponent(); var rect = new Rect(0, 0, bgTex.width, bgTex.height); var sprite = Sprite.Create(bgTex, rect, new Vector2(0.5f, 0.5f), 1); // Clone the material, which is the default UI material, to avoid changing it permanently. image.material = new Material(image.material); image.material.mainTexture = bgTex; image.sprite = sprite; var newColor = image.color; image.color = newColor; image.canvasRenderer.SetAlpha(0.0f); image.CrossFadeAlpha(1.0f, 0.4f, false); var canvas = GetComponentInParent(); _background.transform.localScale = new Vector3(1, 1, 1); _background.GetComponent().sizeDelta = canvas.GetComponent().sizeDelta; _background.transform.SetParent(transform, false); _background.transform.SetAsFirstSibling(); } private void RemoveBackground() { var image = _background.GetComponent(); if (image != null) { image.CrossFadeAlpha(0.0f, 0.2f, false); } } #region 绘制关卡信息部分 private void DrawContent() { chapterContentRect.DestroyAllChildren(); chapterDetail.SetActive(false); continueButton.onClick.AddListener(OnContinueClick); restartButton.onClick.AddListener(OnRestartClick); var chapterList = chapterController.GetChapterList(); if (chapterList.Count <= 0) { errorInfo.gameObject.SetActive(true); } else { errorInfo.gameObject.SetActive(false); foreach (var chapterVo in chapterList) { var chapterItem = Instantiate(chapterItemPrefab, chapterContentRect).GetComponent(); chapterItem.Init(chapterVo); chapterItem.OnClick += OnChapterSelected; } } } private void DestroyContent() { chapterDetail.SetActive(false); _curChapterVo = default; continueButton.onClick.RemoveListener(OnContinueClick); restartButton.onClick.RemoveListener(OnRestartClick); chapterContentRect.DestroyAllChildren(); gameObject.SetActive(false); Destroy(_background); } private void OnChapterSelected(ChapterVo chapterVo) { chapterDetail.SetActive(true); chapterDetailImage.sprite = chapterVo.pic; chapterDetailText.text = chapterVo.desc; continueButton.gameObject.SetActive(chapterVo.hasSaveFile); _curChapterVo = chapterVo; } private void OnContinueClick() { if (!_curChapterVo.hasSaveFile) { Debug.LogWarning("存档与关联篇章不匹配,请查找问题"); } chapterController.StartWithSaveFile(); Close(); } private void OnRestartClick() { chapterController.StartWithChapter(_curChapterVo); Close(); } #endregion #region panel属性 public bool IsOpen => gameObject.activeSelf; public bool IsCloseable => true; public void Close() { UIManager.Instance.HidePanel(); } #endregion } }