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,8 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using BaseGames.Core.Events;
using BaseGames.Localization;
namespace BaseGames.UI
{
@@ -32,6 +34,7 @@ namespace BaseGames.UI
private Vector2 _shownPos;
private Vector2 _hiddenPos;
private Coroutine _slideCoroutine;
private readonly List<float> _pendingThresholds = new();
private readonly CompositeDisposable _subs = new();
private void Awake()
@@ -49,6 +52,7 @@ namespace BaseGames.UI
_onBossHPChanged?.Subscribe(OnHPChanged).AddTo(_subs);
_onBossNameSet?.Subscribe(OnNameSet).AddTo(_subs);
_onBossHPMaxSet?.Subscribe(OnMaxSet).AddTo(_subs);
_onBossPhaseThreshold?.Subscribe(OnPhaseThreshold).AddTo(_subs);
}
private void OnDisable() => _subs.Clear();
@@ -64,6 +68,7 @@ namespace BaseGames.UI
}
else
{
_pendingThresholds.Clear();
_slideCoroutine = StartCoroutine(SlideOut());
}
}
@@ -75,19 +80,52 @@ namespace BaseGames.UI
private void OnNameSet(string bossName)
{
if (_bossNameText != null) _bossNameText.text = bossName;
if (_bossNameText == null) return;
string loc = !string.IsNullOrEmpty(bossName)
? LocalizationManager.Get(bossName, LocalizationTable.Character)
: null;
_bossNameText.text = !string.IsNullOrEmpty(loc) && loc != bossName ? loc : bossName;
}
private void OnMaxSet(int max)
{
_maxHP = max;
// 重建阶段标记(每次 BossHPMax 改变时清空并按已存阈值重建,此处简化为清空)
// 清除旧标记
if (_phaseMarkersRoot != null)
{
// 逆序删除:避免正序枚举 Transform 子节点同时销毁时的迭代器失效
for (int i = _phaseMarkersRoot.childCount - 1; i >= 0; i--)
Destroy(_phaseMarkersRoot.GetChild(i).gameObject);
}
// 延迟一帧:等 Canvas LayoutRebuilder 完成布局后再读取 rect.width避免得到 0
StartCoroutine(RebuildMarkersAfterLayout());
}
private System.Collections.IEnumerator RebuildMarkersAfterLayout()
{
yield return null; // 等待 Canvas 完成当前帧的 Layout 传递
foreach (var t in _pendingThresholds)
PlacePhaseMarker(t);
}
private void OnPhaseThreshold(float threshold)
{
if (threshold <= 0f || threshold >= 1f) return;
_pendingThresholds.Add(threshold);
if (_maxHP > 0) PlacePhaseMarker(threshold);
}
private void PlacePhaseMarker(float threshold)
{
if (_phaseMarkersRoot == null || _phaseMarkerPrefab == null || _hpFill == null) return;
var marker = Instantiate(_phaseMarkerPrefab, _phaseMarkersRoot);
var markerRect = marker.GetComponent<RectTransform>();
if (markerRect != null)
{
float barWidth = _hpFill.rectTransform.rect.width;
markerRect.anchoredPosition = new Vector2(barWidth * threshold, 0f);
}
marker.SetActive(true);
}
// ── 动画协程 ──────────────────────────────────────────────────────────