using UnityEngine; using System.Collections; using TMPro; public class ChestFlyController : MonoBehaviour { public static ChestFlyController Instance; [Header("飞走动画时长")] public float flyDuration = 1.5f; [Header("文字设置")] public TMP_Text flyText; // 关联文字 public float textFadeInTime = 0.5f; // 淡入时间 public float textShowTime = 1.5f; // 显示时长 private Animator animator; private CanvasGroup canvasGroup; private BoatController boat; void Awake() { Instance = this; animator = GetComponentInChildren (); canvasGroup = GetComponent(); canvasGroup.alpha = 0; canvasGroup.blocksRaycasts = false; gameObject.SetActive(false); // 初始隐藏文字 if (flyText != null) { Color c = flyText.color; c.a = 0; flyText.color = c; } } public void PlayFlyAnimation( BoatController boatRef) { boat = boatRef; gameObject.SetActive(true); StartCoroutine(ShowFly()); } IEnumerator ShowFly() { // 暂停敌人 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; // 等待飞走动画播放完 yield return new WaitForSeconds(flyDuration); // 飞走动画结束后显示文字 yield return StartCoroutine(ShowFlyText()); StartCoroutine(HideFly()); } IEnumerator ShowFlyText() { if (flyText == null) yield break; // 文字淡入 float t = 0; while (t < 1f) { t += Time.deltaTime / textFadeInTime; Color c = flyText.color; c.a = Mathf.Clamp01(t); flyText.color = c; yield return null; } // 显示一段时间 yield return new WaitForSeconds( textShowTime ); // 文字淡出 t = 1f; while (t > 0) { t -= Time.deltaTime / textFadeInTime; Color c = flyText.color; c.a = Mathf.Clamp01(t); flyText.color = c; yield return null; } } IEnumerator HideFly() { float t = 1f; while (t > 0) { t -= Time.deltaTime * 3f; canvasGroup.alpha = t; yield return null; } canvasGroup.alpha = 0; canvasGroup.blocksRaycasts = false; // 重置文字透明度 if (flyText != null) { Color c = flyText.color; c.a = 0; flyText.color = c; } gameObject.SetActive(false); // 恢复敌人 GameManager.Instance.ResumeEnemies(); // 恢复小船 if (boat != null) boat.ResumeAfterAnimation(); } }