UI系统优化
This commit is contained in:
@@ -4,6 +4,7 @@ using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
using BaseGames.Core.Events;
|
||||
using BaseGames.Localization;
|
||||
|
||||
namespace BaseGames.UI
|
||||
{
|
||||
@@ -22,6 +23,7 @@ namespace BaseGames.UI
|
||||
|
||||
private CanvasGroup _cg;
|
||||
private Coroutine _hideCoroutine;
|
||||
private WaitForSecondsRealtime _waitDisplay; // 缓存:避免每次 Show 堆分配
|
||||
|
||||
/// <summary>淡入 + 保持 + 淡出的总时长(供 ToastManager 队列计时用)。</summary>
|
||||
public float TotalTime => _displayDuration + _fadeDuration * 2f;
|
||||
@@ -30,6 +32,7 @@ namespace BaseGames.UI
|
||||
{
|
||||
_cg = GetComponent<CanvasGroup>();
|
||||
_cg.alpha = 0f;
|
||||
_waitDisplay = new WaitForSecondsRealtime(_displayDuration);
|
||||
}
|
||||
|
||||
public void Show(string title, string body, Sprite icon = null)
|
||||
@@ -51,8 +54,8 @@ namespace BaseGames.UI
|
||||
{
|
||||
// 淡入
|
||||
yield return StartCoroutine(FadeTo(1f));
|
||||
// 保持
|
||||
yield return new WaitForSecondsRealtime(_displayDuration);
|
||||
// 保持(复用缓存的 WaitForSecondsRealtime)
|
||||
yield return _waitDisplay;
|
||||
// 淡出
|
||||
yield return StartCoroutine(FadeTo(0f));
|
||||
gameObject.SetActive(false);
|
||||
@@ -89,6 +92,14 @@ namespace BaseGames.UI
|
||||
private readonly Queue<(string title, string body, Sprite icon)> _queue = new();
|
||||
private readonly CompositeDisposable _subs = new();
|
||||
private bool _showing;
|
||||
private WaitForSecondsRealtime _queueWait; // 缓存:避免 ProcessQueue 每条堆分配
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// _toast.TotalTime 基于 Inspector 常量,只需 Awake 时初始化一次
|
||||
if (_toast != null)
|
||||
_queueWait = new WaitForSecondsRealtime(_toast.TotalTime + 0.1f);
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
@@ -100,8 +111,11 @@ namespace BaseGames.UI
|
||||
_subs.Clear();
|
||||
}
|
||||
|
||||
private void OnAchievement(string id) => Enqueue("成就解锁", id, null);
|
||||
private void OnAbility(string abilityId) => Enqueue("能力获得", abilityId, null);
|
||||
private void OnAchievement(string id)
|
||||
=> Enqueue(LocalizationManager.Get("TOAST_ACHIEVEMENT_TITLE", LocalizationTable.UI), id, null);
|
||||
|
||||
private void OnAbility(string abilityId)
|
||||
=> Enqueue(LocalizationManager.Get("TOAST_ABILITY_TITLE", LocalizationTable.UI), abilityId, null);
|
||||
|
||||
public void Enqueue(string title, string body, Sprite icon = null)
|
||||
{
|
||||
@@ -115,10 +129,13 @@ namespace BaseGames.UI
|
||||
while (_queue.Count > 0)
|
||||
{
|
||||
var (title, body, icon) = _queue.Dequeue();
|
||||
if (_toast != null) _toast.Show(title, body, icon);
|
||||
// 等待 Toast 完成后再显示下一条(与 ToastNotification._displayDuration/_fadeDuration 保持同步)
|
||||
float wait = _toast != null ? _toast.TotalTime + 0.1f : 3.6f;
|
||||
yield return new WaitForSecondsRealtime(wait);
|
||||
// _toast 为 null 时直接终止队列(不能显示,也无需等待)
|
||||
if (_toast == null) break;
|
||||
_toast.Show(title, body, icon);
|
||||
// 使用缓存的等待对象,_toast 为 null 时已在上方 break
|
||||
if (_queueWait == null)
|
||||
_queueWait = new WaitForSecondsRealtime(_toast.TotalTime + 0.1f);
|
||||
yield return _queueWait;
|
||||
}
|
||||
_showing = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user