94 lines
3.1 KiB
C#
94 lines
3.1 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
|
|
namespace BaseGames.Tutorial
|
|
{
|
|
/// <summary>
|
|
/// 教程提示 UI 组件(架构 21_LiquidPuzzleModule §17)。
|
|
/// 负责显示/隐藏提示面板和文字;duration ≤ 0 时不自动消隐。
|
|
/// 使用 CanvasGroup 实现淡入淡出,不依赖 Animator。
|
|
/// </summary>
|
|
[RequireComponent(typeof(CanvasGroup))]
|
|
public class TutorialHintUI : MonoBehaviour
|
|
{
|
|
[SerializeField] private TMP_Text _label;
|
|
[SerializeField] private float _fadeSpeed = 4f;
|
|
|
|
private CanvasGroup _cg;
|
|
private Coroutine _autoHideCoroutine;
|
|
private Coroutine _fadeCoroutine;
|
|
|
|
private void Awake()
|
|
{
|
|
_cg = GetComponent<CanvasGroup>();
|
|
_cg.alpha = 0f;
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
// ── 公共 API ──────────────────────────────────────────────────────
|
|
/// <summary>淡入显示提示文字。duration ≤ 0 时不自动消隐。</summary>
|
|
public void Show(string text, float duration = 0f)
|
|
{
|
|
if (_label != null) _label.text = text;
|
|
|
|
// 停止所有进行中的动画/定时
|
|
StopAutoHide();
|
|
StopFade();
|
|
|
|
gameObject.SetActive(true);
|
|
_fadeCoroutine = StartCoroutine(FadeTo(1f));
|
|
|
|
if (duration > 0f)
|
|
_autoHideCoroutine = StartCoroutine(AutoHideRoutine(duration));
|
|
}
|
|
|
|
/// <summary>淡出并隐藏提示面板。</summary>
|
|
public void Hide()
|
|
{
|
|
StopAutoHide();
|
|
StopFade();
|
|
_fadeCoroutine = StartCoroutine(FadeOutAndDeactivate());
|
|
}
|
|
|
|
// ── 内部 ──────────────────────────────────────────────────────────
|
|
private void StopAutoHide()
|
|
{
|
|
if (_autoHideCoroutine == null) return;
|
|
StopCoroutine(_autoHideCoroutine);
|
|
_autoHideCoroutine = null;
|
|
}
|
|
|
|
private void StopFade()
|
|
{
|
|
if (_fadeCoroutine == null) return;
|
|
StopCoroutine(_fadeCoroutine);
|
|
_fadeCoroutine = null;
|
|
}
|
|
|
|
private IEnumerator AutoHideRoutine(float duration)
|
|
{
|
|
yield return new WaitForSecondsRealtime(duration);
|
|
Hide();
|
|
_autoHideCoroutine = null;
|
|
}
|
|
|
|
private IEnumerator FadeTo(float target)
|
|
{
|
|
while (!Mathf.Approximately(_cg.alpha, target))
|
|
{
|
|
_cg.alpha = Mathf.MoveTowards(_cg.alpha, target, _fadeSpeed * Time.unscaledDeltaTime);
|
|
yield return null;
|
|
}
|
|
_cg.alpha = target;
|
|
}
|
|
|
|
private IEnumerator FadeOutAndDeactivate()
|
|
{
|
|
yield return FadeTo(0f);
|
|
gameObject.SetActive(false);
|
|
_fadeCoroutine = null;
|
|
}
|
|
}
|
|
}
|