UI系统优化
This commit is contained in:
@@ -7,48 +7,87 @@ namespace BaseGames.Tutorial
|
||||
/// <summary>
|
||||
/// 教程提示 UI 组件(架构 21_LiquidPuzzleModule §17)。
|
||||
/// 负责显示/隐藏提示面板和文字;duration ≤ 0 时不自动消隐。
|
||||
/// 使用 CanvasGroup 实现淡入淡出,不依赖 Animator。
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(CanvasGroup))]
|
||||
public class TutorialHintUI : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject _panel;
|
||||
[SerializeField] private TMP_Text _label;
|
||||
#pragma warning disable CS0414
|
||||
[SerializeField] private float _fadeSpeed = 4f;
|
||||
#pragma warning restore CS0414
|
||||
[SerializeField] private TMP_Text _label;
|
||||
[SerializeField] private float _fadeSpeed = 4f;
|
||||
|
||||
private Coroutine _autoHideCoroutine;
|
||||
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>
|
||||
/// <summary>淡入显示提示文字。duration ≤ 0 时不自动消隐。</summary>
|
||||
public void Show(string text, float duration = 0f)
|
||||
{
|
||||
if (_panel != null) _panel.SetActive(true);
|
||||
if (_label != null) _label.text = text;
|
||||
|
||||
if (_autoHideCoroutine != null)
|
||||
StopCoroutine(_autoHideCoroutine);
|
||||
// 停止所有进行中的动画/定时
|
||||
StopAutoHide();
|
||||
StopFade();
|
||||
|
||||
gameObject.SetActive(true);
|
||||
_fadeCoroutine = StartCoroutine(FadeTo(1f));
|
||||
|
||||
if (duration > 0f)
|
||||
_autoHideCoroutine = StartCoroutine(AutoHideRoutine(duration));
|
||||
}
|
||||
|
||||
/// <summary>立即隐藏提示面板。</summary>
|
||||
/// <summary>淡出并隐藏提示面板。</summary>
|
||||
public void Hide()
|
||||
{
|
||||
if (_autoHideCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_autoHideCoroutine);
|
||||
_autoHideCoroutine = null;
|
||||
}
|
||||
if (_panel != null) _panel.SetActive(false);
|
||||
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 WaitForSeconds(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user