using AibisDream.Kit; using AibisDream.Utility; using TMPro; using UnityEngine; using UnityEngine.UI; namespace AibisDream.UI { public class ChapterPanel : MonoBehaviour, IUIPanel { #region 索引 public ChapterController chapterController; public Animator animator; public Image background; public RectTransform chapterContentRect; public TMP_Text errorInfo; public GameObject chapterDetail; public TMP_Text chapterDetailText; public Button continueButton; public Button restartButton; public GameObject chapterItemPrefab; #endregion 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() { background.canvasRenderer.SetAlpha(0.0f); background.gameObject.SetActive(true); background.CrossFadeAlpha(1.0f, 0.4f, false); } private void RemoveBackground() { background.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); background.gameObject.SetActive(false); } private void OnChapterSelected(ChapterVo chapterVo) { chapterDetail.SetActive(true); 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 } }