using UnityEngine; using System.Collections; public class ChestUIController : MonoBehaviour { public static ChestUIController Instance; [Header("动画播放完等待几秒后关闭")] public float closeDelay = 0.3f; [Header("关联对象")] public GameObject chestOpenObj; public GameObject chestFlyObj; private Animator openAnimator; private CanvasGroup canvasGroup; private BoatController boat; void Awake() { Instance = this; openAnimator = chestOpenObj .GetComponent(); canvasGroup = GetComponent(); canvasGroup.alpha = 0; canvasGroup.blocksRaycasts = false; gameObject.SetActive(false); } public void PlayChestAnimation( BoatController boatRef) { boat = boatRef; gameObject.SetActive(true); StartCoroutine(ShowChest()); } IEnumerator ShowChest() { // 暂停敌人 GameManager.Instance.PauseEnemies(); canvasGroup.blocksRaycasts = true; float t = 0; while (t < 1f) { t += Time.deltaTime * 3f; canvasGroup.alpha = t; yield return null; } canvasGroup.alpha = 1f; openAnimator.SetTrigger("Open"); yield return null; float animLength = openAnimator .GetCurrentAnimatorStateInfo(0).length; yield return new WaitForSeconds( animLength + closeDelay ); StartCoroutine(HideChest()); } IEnumerator HideChest() { float t = 1f; while (t > 0) { t -= Time.deltaTime * 3f; canvasGroup.alpha = t; yield return null; } canvasGroup.alpha = 0; canvasGroup.blocksRaycasts = false; gameObject.SetActive(false); // 恢复敌人 GameManager.Instance.ResumeEnemies(); // 恢复小船 if (boat != null) boat.ResumeAfterAnimation(); // 检查是否收集完所有宝箱 if (CollectManager.Instance != null && CollectManager.Instance .IsAllCollected()) { Debug.Log( "✅ 最后一个动画播完,触发胜利!" ); GameManager.Instance.WinGame(); } } }