UI系统优化

This commit is contained in:
2026-05-25 11:54:37 +08:00
parent c7057db27d
commit 3c812cfb41
130 changed files with 4738 additions and 477 deletions

View File

@@ -1,7 +1,9 @@
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using BaseGames.Core;
using BaseGames.Core.Events;
using BaseGames.Equipment;
namespace BaseGames.UI
{
@@ -11,37 +13,67 @@ namespace BaseGames.UI
/// </summary>
public class ToolHUD : MonoBehaviour
{
[SerializeField] private ToolSlotUI[] _slots; // 2 个 ToolSlotUI 组件
[SerializeField] private BaseGames.Equipment.ToolSlotManager _slotManager;
[SerializeField] private ToolUsedEventChannelSO _onToolUsed;
[SerializeField] private ToolSlotUI[] _slots; // 2 个 ToolSlotUI 组件
[SerializeField] private ToolUsedEventChannelSO _onToolUsed;
// 通过 ServiceLocator 解析:避免 UI 直接持有 ToolSlotManager 具体类型引用
private IToolSlotService _slotManager;
private readonly CompositeDisposable _subs = new();
// 每个槽独立的冷却追踪协程;仅在冷却期间运行,避免每帧全槽轮询
private Coroutine[] _cooldownCoroutines;
private void Awake()
{
_cooldownCoroutines = _slots != null
? new Coroutine[_slots.Length]
: System.Array.Empty<Coroutine>();
}
private void OnEnable()
{
_slotManager = ServiceLocator.GetOrDefault<IToolSlotService>();
_onToolUsed?.Subscribe(RefreshSlot).AddTo(_subs);
}
private void OnDisable()
{
_subs.Clear();
StopAllCoroutines();
if (_cooldownCoroutines != null)
for (int i = 0; i < _cooldownCoroutines.Length; i++)
_cooldownCoroutines[i] = null;
}
private void RefreshSlot(ToolUsedPayload payload)
{
int i = payload.SlotIndex;
if (_slots == null || i < 0 || i >= _slots.Length) return;
if (_slotManager == null) return; // ToolSlotManager 尚未注册
_slots[i].Refresh(
_slotManager.GetTool(i),
_slotManager.GetRemainingUses(i),
_slotManager.GetCooldownRatio(i));
// 启动(或重启)该槽的冷却追踪协程
if (_cooldownCoroutines[i] != null) StopCoroutine(_cooldownCoroutines[i]);
_cooldownCoroutines[i] = StartCoroutine(TrackCooldown(i));
}
private void Update()
/// <summary>
/// 仅在该槽冷却未结束时每帧更新填充值;冷却归零后自动停止,不消耗额外 CPU。
/// </summary>
private System.Collections.IEnumerator TrackCooldown(int slotIndex)
{
if (_slots == null || _slotManager == null) return;
for (int i = 0; i < _slots.Length; i++)
_slots[i].SetCooldownFill(_slotManager.GetCooldownRatio(i));
while (_slotManager != null && _slotManager.GetCooldownRatio(slotIndex) > 0f)
{
_slots[slotIndex].SetCooldownFill(_slotManager.GetCooldownRatio(slotIndex));
yield return null;
}
// 冷却结束,确保填充归零
if (slotIndex < _slots.Length)
_slots[slotIndex].SetCooldownFill(0f);
_cooldownCoroutines[slotIndex] = null;
}
}