UI相关优化补充
This commit is contained in:
@@ -20,7 +20,9 @@
|
||||
"BaseGames.Combat.StatusEffects",
|
||||
"BaseGames.Spells",
|
||||
"BaseGames.Quest",
|
||||
"BaseGames.Skills"
|
||||
"BaseGames.Skills",
|
||||
"Unity.Addressables",
|
||||
"Unity.ResourceManager"
|
||||
],
|
||||
"autoReferenced": true,
|
||||
"overrideReferences": false,
|
||||
|
||||
95
Assets/_Game/Scripts/UI/CharmCardView.cs
Normal file
95
Assets/_Game/Scripts/UI/CharmCardView.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
using BaseGames.Equipment;
|
||||
using BaseGames.Localization;
|
||||
|
||||
namespace BaseGames.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// 护符卡片视图组件。
|
||||
///
|
||||
/// 设计动机:取代 <see cref="CharmEquipPanel"/> 中基于
|
||||
/// <see cref="Component.GetComponentsInChildren{T}(bool)"/> + 数组索引的脆弱绑定方式,
|
||||
/// 通过 Inspector 显式序列化引用,避免每次重建列表时的反射开销与索引漂移风险。
|
||||
///
|
||||
/// 用法:在卡片 Prefab 上挂载此组件并连接子节点引用,<see cref="CharmEquipPanel"/>
|
||||
/// 仅需调用 <see cref="Bind"/> 即可完成数据 → UI 的同步。
|
||||
/// </summary>
|
||||
[DisallowMultipleComponent]
|
||||
public class CharmCardView : MonoBehaviour
|
||||
{
|
||||
[Header("Visual")]
|
||||
[SerializeField] private Image _icon;
|
||||
[SerializeField] private GameObject _iconRoot; // 可选:无 icon 时整体隐藏
|
||||
|
||||
[Header("Text")]
|
||||
[SerializeField] private TMP_Text _nameText;
|
||||
[SerializeField] private TMP_Text _notchCostText;
|
||||
[SerializeField] private TMP_Text _descriptionText;
|
||||
[SerializeField] private TMP_Text _equipBadgeText; // 已装备角标 ("✓")
|
||||
|
||||
[Header("Interaction")]
|
||||
[SerializeField] private Button _actionButton;
|
||||
|
||||
/// <summary>
|
||||
/// 将护符数据绑定到视图。
|
||||
/// </summary>
|
||||
/// <param name="charm">数据源。</param>
|
||||
/// <param name="isEquipped">是否已装备(决定按钮行为与角标显示)。</param>
|
||||
/// <param name="onEquip">点击装备时回调(仅 <paramref name="isEquipped"/> = false 时使用)。</param>
|
||||
/// <param name="onUnequip">点击卸下时回调(仅 <paramref name="isEquipped"/> = true 时使用)。</param>
|
||||
public void Bind(CharmSO charm,
|
||||
bool isEquipped,
|
||||
Action<CharmSO> onEquip,
|
||||
Action<CharmSO> onUnequip)
|
||||
{
|
||||
if (charm == null) return;
|
||||
|
||||
// ── 图标 ────────────────────────────────────────────────────────
|
||||
if (_icon != null)
|
||||
{
|
||||
_icon.sprite = charm.icon;
|
||||
_icon.enabled = charm.icon != null;
|
||||
}
|
||||
if (_iconRoot != null) _iconRoot.SetActive(charm.icon != null);
|
||||
|
||||
// ── 文本(本地化容错:未找到键时回落到 charmId / 空字符串)──────
|
||||
if (_nameText != null)
|
||||
{
|
||||
string loc = LocalizationManager.Get(charm.displayNameKey, LocalizationTable.Items);
|
||||
_nameText.text = !string.IsNullOrEmpty(loc) && loc != charm.displayNameKey
|
||||
? loc : charm.charmId;
|
||||
}
|
||||
if (_notchCostText != null)
|
||||
_notchCostText.text = charm.notchCost.ToString();
|
||||
|
||||
if (_descriptionText != null)
|
||||
{
|
||||
string loc = LocalizationManager.Get(charm.descriptionKey, LocalizationTable.Items);
|
||||
_descriptionText.text = !string.IsNullOrEmpty(loc) && loc != charm.descriptionKey
|
||||
? loc : string.Empty;
|
||||
}
|
||||
if (_equipBadgeText != null)
|
||||
_equipBadgeText.text = isEquipped ? "✓" : string.Empty;
|
||||
|
||||
// ── 按钮 ────────────────────────────────────────────────────────
|
||||
if (_actionButton != null)
|
||||
{
|
||||
_actionButton.onClick.RemoveAllListeners();
|
||||
var captured = charm;
|
||||
if (isEquipped)
|
||||
{
|
||||
if (onUnequip != null)
|
||||
_actionButton.onClick.AddListener(() => onUnequip(captured));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (onEquip != null)
|
||||
_actionButton.onClick.AddListener(() => onEquip(captured));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/_Game/Scripts/UI/CharmCardView.cs.meta
Normal file
11
Assets/_Game/Scripts/UI/CharmCardView.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0aa9daa814d61c94aaed9b6db2ac1fdf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -139,12 +139,38 @@ namespace BaseGames.UI
|
||||
go.transform.SetParent(parent, worldPositionStays: false);
|
||||
go.SetActive(true);
|
||||
|
||||
// Icon(第一个 Image)
|
||||
// 优先使用预绑定视图组件(推荐);若 Prefab 未挂载则回落到反射查找以保证向后兼容。
|
||||
var view = go.GetComponent<CharmCardView>();
|
||||
if (view != null)
|
||||
{
|
||||
view.Bind(charm, isEquipped, OnEquipClicked, OnUnequipClicked);
|
||||
}
|
||||
else
|
||||
{
|
||||
FallbackBindByReflection(go, charm, isEquipped);
|
||||
}
|
||||
|
||||
return go;
|
||||
}
|
||||
|
||||
private void OnEquipClicked(CharmSO charm)
|
||||
{
|
||||
string err = _manager?.TryEquipCharm(charm);
|
||||
if (err != null) Debug.LogWarning($"[CharmEquipPanel] 装备失败: {err}");
|
||||
}
|
||||
|
||||
private void OnUnequipClicked(CharmSO charm) => _manager?.UnequipCharm(charm);
|
||||
|
||||
/// <summary>
|
||||
/// 兼容旧 Prefab(未挂载 <see cref="CharmCardView"/>)的反射绑定。
|
||||
/// 仅作为过渡方案;首次开发完成后建议在 Editor 中强制要求新组件。
|
||||
/// </summary>
|
||||
private void FallbackBindByReflection(GameObject go, CharmSO charm, bool isEquipped)
|
||||
{
|
||||
var images = go.GetComponentsInChildren<Image>(includeInactive: true);
|
||||
if (images.Length > 0 && charm.icon != null)
|
||||
images[0].sprite = charm.icon;
|
||||
|
||||
// 文本(约定:TMP_Text[0] = 名称, TMP_Text[1] = 凹槽消耗, TMP_Text[2] = 描述)
|
||||
var texts = go.GetComponentsInChildren<TMP_Text>(includeInactive: true);
|
||||
if (texts.Length > 0)
|
||||
{
|
||||
@@ -152,9 +178,7 @@ namespace BaseGames.UI
|
||||
texts[0].text = string.IsNullOrEmpty(name) || name == charm.displayNameKey
|
||||
? charm.charmId : name;
|
||||
}
|
||||
if (texts.Length > 1)
|
||||
texts[1].text = charm.notchCost.ToString();
|
||||
|
||||
if (texts.Length > 1) texts[1].text = charm.notchCost.ToString();
|
||||
if (texts.Length > 2)
|
||||
{
|
||||
string desc = LocalizationManager.Get(charm.descriptionKey, LocalizationTable.Items);
|
||||
@@ -162,34 +186,18 @@ namespace BaseGames.UI
|
||||
? string.Empty : desc;
|
||||
}
|
||||
|
||||
// 按钮(约定:第一个 Button = 装备/卸下切换)
|
||||
var btn = go.GetComponentInChildren<Button>(includeInactive: true);
|
||||
if (btn != null)
|
||||
{
|
||||
btn.onClick.RemoveAllListeners();
|
||||
CharmSO captured = charm;
|
||||
bool equip = !isEquipped;
|
||||
|
||||
if (equip)
|
||||
{
|
||||
btn.onClick.AddListener(() =>
|
||||
{
|
||||
string err = _manager?.TryEquipCharm(captured);
|
||||
if (err != null)
|
||||
Debug.LogWarning($"[CharmEquipPanel] 装备失败: {err}");
|
||||
});
|
||||
}
|
||||
if (!isEquipped)
|
||||
btn.onClick.AddListener(() => OnEquipClicked(captured));
|
||||
else
|
||||
{
|
||||
btn.onClick.AddListener(() => _manager?.UnequipCharm(captured));
|
||||
}
|
||||
|
||||
// 已装备时使用不同视觉(可选:标记文本)
|
||||
btn.onClick.AddListener(() => OnUnequipClicked(captured));
|
||||
if (texts.Length > 3)
|
||||
texts[3].text = isEquipped ? "✓" : string.Empty;
|
||||
}
|
||||
|
||||
return go;
|
||||
}
|
||||
|
||||
// ── 回收 ─────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -9,8 +9,8 @@ namespace BaseGames.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// 伤害飘字(架构 10_UIModule §10)。
|
||||
/// 从对象池取出后由 FloatingDamageSpawner 调用 Show();协程完成后归还。
|
||||
/// 挂在预制体根 GameObject 上(Canvas_HUD 子节点,World Space Canvas 或 Screen Space)。
|
||||
/// 从对象池取出后由 FloatingDamageSpawner 调用 Show();
|
||||
/// 动画推进由 <see cref="FloatingDamageTickSystem"/> 统一调度,避免大量独立协程。
|
||||
/// </summary>
|
||||
public class FloatingDamageText : MonoBehaviour
|
||||
{
|
||||
@@ -26,8 +26,7 @@ namespace BaseGames.UI
|
||||
[SerializeField] private Canvas _parentCanvas;
|
||||
|
||||
private RectTransform _rectTransform;
|
||||
private Coroutine _animCoroutine;
|
||||
// 每次 Show() 解析一次,协程期间(< 1s)复用,避免每帧走 FindObjectByTag
|
||||
// 每次 Show() 解析一次,动画期间(< 1s)复用,避免每帧走 FindObjectByTag
|
||||
private Camera _cachedCamera;
|
||||
|
||||
private void Awake()
|
||||
@@ -36,15 +35,15 @@ namespace BaseGames.UI
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在世界坐标位置显示伤害数字并开始飘动动画。
|
||||
/// 动画结束后由对象池回收(Deactivate)。
|
||||
/// 在世界坐标位置显示伤害数字并启动飘动动画。
|
||||
/// 动画结束后由 OnTickComplete 回调列表 SetActive(false)。
|
||||
/// </summary>
|
||||
public void Show(Vector2 worldPosition, int damage, DamageType type)
|
||||
{
|
||||
if (_animCoroutine != null) StopCoroutine(_animCoroutine);
|
||||
// 取消上一条动画(如果被预期复用)
|
||||
FloatingDamageTickSystem.Instance.Unregister(this);
|
||||
|
||||
// 每次 Show 解析一次摄像机:动画时长 < 1s,期间不会切换主摄像机;
|
||||
// 若 Boss 过场后再次 Show,会自动获取新的主摄像机。
|
||||
// 每次 Show 解析一次摄像机
|
||||
_cachedCamera = (_parentCanvas != null && _parentCanvas.renderMode == RenderMode.ScreenSpaceCamera)
|
||||
? _parentCanvas.worldCamera
|
||||
: UnityEngine.Camera.main;
|
||||
@@ -53,9 +52,22 @@ namespace BaseGames.UI
|
||||
_text.color = GetColorForType(type);
|
||||
|
||||
SetAnchoredPosition(worldPosition);
|
||||
_animCoroutine = StartCoroutine(FloatAndFade(worldPosition));
|
||||
FloatingDamageTickSystem.Instance.Register(this, worldPosition,
|
||||
_duration, _floatDistance, _text.color.a);
|
||||
}
|
||||
|
||||
/// <summary>由 <see cref="FloatingDamageTickSystem"/> 每帧调用,t 为归一化进度 [0,1]。</summary>
|
||||
internal void TickAnimation(Vector2 startWorld, float t, float startAlpha, float floatDistance)
|
||||
{
|
||||
SetAnchoredPosition(startWorld + new Vector2(0, floatDistance * t));
|
||||
var color = _text.color;
|
||||
color.a = Mathf.Lerp(startAlpha, 0f, Mathf.Clamp01((t - 0.5f) / 0.5f));
|
||||
_text.color = color;
|
||||
}
|
||||
|
||||
/// <summary>动画结束回调:反激活 GameObject 归还池。</summary>
|
||||
internal void OnTickComplete() => gameObject.SetActive(false);
|
||||
|
||||
private void SetAnchoredPosition(Vector2 worldPosition)
|
||||
{
|
||||
var cam = _cachedCamera;
|
||||
@@ -82,6 +94,7 @@ namespace BaseGames.UI
|
||||
|
||||
private IEnumerator FloatAndFade(Vector2 startWorld)
|
||||
{
|
||||
// 保留作为 fallback:若业务方选择继续使用独立协程可直接调用本方法。
|
||||
float elapsed = 0f;
|
||||
var color = _text.color;
|
||||
var startAlpha = color.a;
|
||||
@@ -89,18 +102,11 @@ namespace BaseGames.UI
|
||||
while (elapsed < _duration)
|
||||
{
|
||||
float t = elapsed / _duration;
|
||||
|
||||
SetAnchoredPosition(startWorld + new Vector2(0, _floatDistance * t));
|
||||
|
||||
// alpha 淡出(后半段开始)—— 修改 struct 的 a 分量并回写,避免每帧 new Color 堆分配
|
||||
color.a = Mathf.Lerp(startAlpha, 0f, Mathf.Clamp01((t - 0.5f) / 0.5f));
|
||||
_text.color = color;
|
||||
|
||||
TickAnimation(startWorld, t, startAlpha, _floatDistance);
|
||||
elapsed += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
gameObject.SetActive(false); // 归还对象池
|
||||
OnTickComplete();
|
||||
}
|
||||
|
||||
private static Color GetColorForType(DamageType type) => type switch
|
||||
|
||||
102
Assets/_Game/Scripts/UI/FloatingDamageTickSystem.cs
Normal file
102
Assets/_Game/Scripts/UI/FloatingDamageTickSystem.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BaseGames.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// 全局伤害飘字 Tick 系统(架构 10_UIModule §10 优化)。
|
||||
///
|
||||
/// 设计动机:原实现中每条飘字独立 StartCoroutine(FloatAndFade),
|
||||
/// Boss 战或大范围 AOE 时同时存在数十条会产生大量协程对象与调度开销。
|
||||
/// 本系统用单一 Update + 紧凑 List<Entry> 集中推进,
|
||||
/// 所有飘字共享同一份每帧调度(仅一次 Update 调用),并消除协程对象。
|
||||
///
|
||||
/// 使用:FloatingDamageText.Show 不再 StartCoroutine,而是调用
|
||||
/// FloatingDamageTickSystem.Register(this, ...);系统自动在动画结束时
|
||||
/// 调用 OnComplete 回调(飘字 SetActive(false) 回池)。
|
||||
/// </summary>
|
||||
[DefaultExecutionOrder(+100)]
|
||||
public class FloatingDamageTickSystem : MonoBehaviour
|
||||
{
|
||||
private struct Entry
|
||||
{
|
||||
public FloatingDamageText Target;
|
||||
public Vector2 StartWorld;
|
||||
public float Elapsed;
|
||||
public float Duration;
|
||||
public float FloatDistance;
|
||||
public float StartAlpha;
|
||||
}
|
||||
|
||||
private static FloatingDamageTickSystem s_instance;
|
||||
private readonly List<Entry> _entries = new(64);
|
||||
|
||||
public static FloatingDamageTickSystem Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (s_instance != null) return s_instance;
|
||||
var go = new GameObject(nameof(FloatingDamageTickSystem));
|
||||
DontDestroyOnLoad(go);
|
||||
s_instance = go.AddComponent<FloatingDamageTickSystem>();
|
||||
return s_instance;
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (s_instance != null && s_instance != this) { Destroy(this); return; }
|
||||
s_instance = this;
|
||||
}
|
||||
|
||||
public void Register(FloatingDamageText target, Vector2 startWorld,
|
||||
float duration, float floatDistance, float startAlpha)
|
||||
{
|
||||
if (target == null || duration <= 0f) return;
|
||||
_entries.Add(new Entry
|
||||
{
|
||||
Target = target,
|
||||
StartWorld = startWorld,
|
||||
Elapsed = 0f,
|
||||
Duration = duration,
|
||||
FloatDistance = floatDistance,
|
||||
StartAlpha = startAlpha
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>取消指定目标的飘字动画(例如对象池回收)。</summary>
|
||||
public void Unregister(FloatingDamageText target)
|
||||
{
|
||||
for (int i = _entries.Count - 1; i >= 0; i--)
|
||||
if (_entries[i].Target == target) _entries.RemoveAt(i);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
float dt = Time.deltaTime;
|
||||
// 倒序遍历,便于安全 RemoveAt
|
||||
for (int i = _entries.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var e = _entries[i];
|
||||
if (e.Target == null) { _entries.RemoveAt(i); continue; }
|
||||
|
||||
e.Elapsed += dt;
|
||||
float t = e.Elapsed / e.Duration;
|
||||
bool done = t >= 1f;
|
||||
if (done) t = 1f;
|
||||
|
||||
e.Target.TickAnimation(e.StartWorld, t, e.StartAlpha, e.FloatDistance);
|
||||
|
||||
if (done)
|
||||
{
|
||||
e.Target.OnTickComplete();
|
||||
_entries.RemoveAt(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
_entries[i] = e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/_Game/Scripts/UI/FloatingDamageTickSystem.cs.meta
Normal file
11
Assets/_Game/Scripts/UI/FloatingDamageTickSystem.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f56b6630ca8a34944aa71535b4c68574
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -131,21 +131,11 @@ namespace BaseGames.UI
|
||||
// ── 动画协程 ──────────────────────────────────────────────────────────
|
||||
|
||||
private IEnumerator SlideTo(Vector2 target)
|
||||
{
|
||||
Vector2 start = _rect.anchoredPosition;
|
||||
float t = 0;
|
||||
while (t < _slideDuration)
|
||||
{
|
||||
_rect.anchoredPosition = Vector2.Lerp(start, target, t / _slideDuration);
|
||||
t += Time.unscaledDeltaTime;
|
||||
yield return null;
|
||||
}
|
||||
_rect.anchoredPosition = target;
|
||||
}
|
||||
=> UITween.MoveAnchored(_rect, target, _slideDuration);
|
||||
|
||||
private IEnumerator SlideOut()
|
||||
{
|
||||
yield return SlideTo(_hiddenPos);
|
||||
yield return UITween.MoveAnchored(_rect, _hiddenPos, _slideDuration);
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Unity.Profiling;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
@@ -47,6 +48,10 @@ namespace BaseGames.UI.HUD
|
||||
private readonly CompositeDisposable _subs = new();
|
||||
private int _lastLingZhu = int.MinValue;
|
||||
|
||||
// ── 性能基线 (Profiler 窗口可在 "Scripts" 类别下看到) ──
|
||||
private static readonly ProfilerMarker s_RebuildHPMarker = new("HUD.RebuildHPCells");
|
||||
private static readonly ProfilerMarker s_RebuildSpringMarker = new("HUD.RebuildSpringIcons");
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_onHPChanged?.Subscribe(UpdateHP).AddTo(_subs);
|
||||
@@ -69,6 +74,7 @@ namespace BaseGames.UI.HUD
|
||||
|
||||
private void RebuildHPCells(int max)
|
||||
{
|
||||
using var _ = s_RebuildHPMarker.Auto();
|
||||
if (_hpContainer == null || _hpCellPrefab == null) return;
|
||||
// 复用现有 Cell,仅在数量不足时 Instantiate 补充,超出时 SetActive(false) 而非 Destroy
|
||||
for (int i = 0; i < max; i++)
|
||||
@@ -101,6 +107,7 @@ namespace BaseGames.UI.HUD
|
||||
|
||||
private void RebuildSpringIcons(int charges)
|
||||
{
|
||||
using var _ = s_RebuildSpringMarker.Auto();
|
||||
if (_springContainer == null || _springIconPrefab == null) return;
|
||||
// 复用已有图标,超出数量时 SetActive(false)
|
||||
for (int i = 0; i < charges; i++)
|
||||
|
||||
@@ -68,11 +68,26 @@ namespace BaseGames.UI
|
||||
|
||||
// ── 内部 ─────────────────────────────────────────────────────────────
|
||||
|
||||
// 缓存等待对象以避免典型路径(剩余时间 == _minDisplayTime)下的重复分配。
|
||||
private WaitForSecondsRealtime _cachedFullWait;
|
||||
|
||||
private IEnumerator HideAfterMinTime()
|
||||
{
|
||||
float elapsed = Time.unscaledTime - _shownAt;
|
||||
if (elapsed < _minDisplayTime)
|
||||
yield return new WaitForSecondsRealtime(_minDisplayTime - elapsed);
|
||||
float elapsed = Time.unscaledTime - _shownAt;
|
||||
float remaining = _minDisplayTime - elapsed;
|
||||
if (remaining > 0f)
|
||||
{
|
||||
// 完整剩余 ≈ 显示时间时复用缓存对象;否则按需新建(罕见路径)。
|
||||
if (Mathf.Approximately(remaining, _minDisplayTime))
|
||||
{
|
||||
_cachedFullWait ??= new WaitForSecondsRealtime(_minDisplayTime);
|
||||
yield return _cachedFullWait;
|
||||
}
|
||||
else
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(remaining);
|
||||
}
|
||||
}
|
||||
if (_loadingRoot != null) _loadingRoot.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,12 @@ namespace BaseGames.UI
|
||||
[SerializeField] private Toggle _vSyncToggle;
|
||||
[SerializeField] private TMP_Dropdown _fpsDropdown; // 30 / 60 / 120 / 无限
|
||||
|
||||
[Header("可访问性")]
|
||||
[SerializeField] private Slider _uiScaleSlider; // 0.8 ~ 1.5
|
||||
[SerializeField] private TMP_Text _uiScaleValueText; // 实时显示 "100%"
|
||||
[SerializeField] private TMP_Dropdown _colorblindDropdown; // None / Prot / Deut / Trit
|
||||
[SerializeField] private Toggle _screenShakeToggle;
|
||||
|
||||
[Header("按键重绑定")]
|
||||
[SerializeField] private GameObject _rebindPanelRoot; // RebindPanel GameObject
|
||||
|
||||
@@ -59,10 +65,46 @@ namespace BaseGames.UI
|
||||
_settings.SetTargetFrameRate(FpsOptions[Mathf.Clamp(i, 0, FpsOptions.Length - 1)]));
|
||||
}
|
||||
|
||||
// ── 可访问性 ──────────────────────────────────────────────────────
|
||||
if (_uiScaleSlider != null)
|
||||
{
|
||||
_uiScaleSlider.onValueChanged.RemoveAllListeners();
|
||||
_uiScaleSlider.minValue = 0.8f;
|
||||
_uiScaleSlider.maxValue = 1.5f;
|
||||
_uiScaleSlider.value = Mathf.Clamp(data.UIScale, _uiScaleSlider.minValue, _uiScaleSlider.maxValue);
|
||||
UpdateUIScaleLabel(_uiScaleSlider.value);
|
||||
_uiScaleSlider.onValueChanged.AddListener(v =>
|
||||
{
|
||||
_settings.SetUIScale(v);
|
||||
UpdateUIScaleLabel(v);
|
||||
});
|
||||
}
|
||||
|
||||
if (_colorblindDropdown != null)
|
||||
{
|
||||
_colorblindDropdown.onValueChanged.RemoveAllListeners();
|
||||
_colorblindDropdown.value = (int)data.ColorblindMode;
|
||||
_colorblindDropdown.onValueChanged.AddListener(i =>
|
||||
_settings.SetColorblindMode((ColorblindMode)Mathf.Clamp(i, 0, 3)));
|
||||
}
|
||||
|
||||
if (_screenShakeToggle != null)
|
||||
{
|
||||
_screenShakeToggle.onValueChanged.RemoveAllListeners();
|
||||
_screenShakeToggle.isOn = data.ScreenShakeEnabled;
|
||||
_screenShakeToggle.onValueChanged.AddListener(v => _settings.SetScreenShakeEnabled(v));
|
||||
}
|
||||
|
||||
// 手柄导航:打开设置面板时将焦点置于主音量滑条
|
||||
EventSystem.current?.SetSelectedGameObject(_masterVolume?.gameObject);
|
||||
}
|
||||
|
||||
private void UpdateUIScaleLabel(float v)
|
||||
{
|
||||
if (_uiScaleValueText != null)
|
||||
_uiScaleValueText.text = Mathf.RoundToInt(v * 100f) + "%";
|
||||
}
|
||||
|
||||
// ── 辅助 ──────────────────────────────────────────────────────────────
|
||||
|
||||
private static void InitSlider(Slider slider, float value, UnityEngine.Events.UnityAction<float> onChange)
|
||||
|
||||
128
Assets/_Game/Scripts/UI/SubtitleManager.cs
Normal file
128
Assets/_Game/Scripts/UI/SubtitleManager.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using BaseGames.Core;
|
||||
using BaseGames.Core.Events;
|
||||
|
||||
namespace BaseGames.UI
|
||||
{
|
||||
/// <summary>屏幕阅读器优先级(与 WCAG / ARIA live-region 等级对齐)。</summary>
|
||||
public enum ScreenReaderPriority
|
||||
{
|
||||
/// <summary>Polite —— 当前播报结束后再播报,不打断玩家。</summary>
|
||||
Polite = 0,
|
||||
/// <summary>Assertive —— 立即打断当前播报(如战斗死亡、关键提示)。</summary>
|
||||
Assertive = 1
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 屏幕阅读器接口(无障碍)。默认无实现;接入第三方 TTS / 平台 API(iOS VoiceOver、
|
||||
/// Android TalkBack、Steam Overlay TTS)时实现本接口并通过 ServiceLocator 注册。
|
||||
/// 设计为 string-only 接口而非富文本,避免不同 TTS 引擎对标签解析不一致。
|
||||
/// </summary>
|
||||
public interface IScreenReader
|
||||
{
|
||||
void Speak(string text, ScreenReaderPriority priority = ScreenReaderPriority.Polite);
|
||||
void StopAll();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 字幕管理器(架构 10_UIModule §11 无障碍补完)。
|
||||
///
|
||||
/// 职责:与对话气泡(DialogueUI)解耦,专门承担"叙述性字幕 / 关键提示文本 / 战斗播报"。
|
||||
/// 订阅 <see cref="StringEventChannelSO"/>(推荐项目内新建 EVT_SubtitleShow)显示底部居中字幕;
|
||||
/// 若 <see cref="ServiceLocator"/> 中存在 <see cref="IScreenReader"/> 实现则同步触发 TTS。
|
||||
///
|
||||
/// 队列策略:Polite 串行(FIFO);Assertive 立即清空队列并显示。
|
||||
/// </summary>
|
||||
public class SubtitleManager : MonoBehaviour
|
||||
{
|
||||
[Header("UI")]
|
||||
[SerializeField] private GameObject _root;
|
||||
[SerializeField] private TMP_Text _label;
|
||||
[SerializeField] private CanvasGroup _canvasGroup;
|
||||
|
||||
[Header("时长")]
|
||||
[Tooltip("每行字幕显示时间(秒);可被 Show(text, durationOverride) 覆盖。")]
|
||||
[SerializeField] private float _defaultDuration = 3f;
|
||||
[SerializeField] private float _fadeDuration = 0.2f;
|
||||
|
||||
[Header("事件频道(可选)")]
|
||||
[SerializeField] private StringEventChannelSO _onSubtitleShow;
|
||||
|
||||
private readonly Queue<(string text, float duration, ScreenReaderPriority pri)> _queue = new();
|
||||
private Coroutine _playCo;
|
||||
private readonly CompositeDisposable _subs = new();
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (_root != null) _root.SetActive(false);
|
||||
if (_canvasGroup != null) _canvasGroup.alpha = 0f;
|
||||
_onSubtitleShow?.Subscribe(t => Show(t, _defaultDuration, ScreenReaderPriority.Polite)).AddTo(_subs);
|
||||
}
|
||||
|
||||
private void OnDisable() => _subs.Clear();
|
||||
|
||||
/// <summary>显示一行字幕;自动按优先级排队。</summary>
|
||||
public void Show(string text, float duration = -1f,
|
||||
ScreenReaderPriority priority = ScreenReaderPriority.Polite)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text)) return;
|
||||
float d = duration > 0f ? duration : _defaultDuration;
|
||||
|
||||
if (priority == ScreenReaderPriority.Assertive)
|
||||
{
|
||||
_queue.Clear();
|
||||
if (_playCo != null) StopCoroutine(_playCo);
|
||||
_playCo = null;
|
||||
}
|
||||
_queue.Enqueue((text, d, priority));
|
||||
|
||||
// 同步触发 TTS
|
||||
var reader = ServiceLocator.GetOrDefault<IScreenReader>(null);
|
||||
reader?.Speak(text, priority);
|
||||
|
||||
if (_playCo == null) _playCo = StartCoroutine(PlayLoop());
|
||||
}
|
||||
|
||||
private IEnumerator PlayLoop()
|
||||
{
|
||||
while (_queue.Count > 0)
|
||||
{
|
||||
var (text, d, _) = _queue.Dequeue();
|
||||
yield return ShowOne(text, d);
|
||||
}
|
||||
_playCo = null;
|
||||
}
|
||||
|
||||
private IEnumerator ShowOne(string text, float duration)
|
||||
{
|
||||
if (_label != null) _label.text = text;
|
||||
if (_root != null) _root.SetActive(true);
|
||||
|
||||
yield return Fade(0f, 1f, _fadeDuration);
|
||||
yield return new WaitForSecondsRealtime(Mathf.Max(0f, duration - _fadeDuration * 2f));
|
||||
yield return Fade(1f, 0f, _fadeDuration);
|
||||
|
||||
if (_root != null) _root.SetActive(false);
|
||||
}
|
||||
|
||||
private IEnumerator Fade(float from, float to, float dur)
|
||||
{
|
||||
if (_canvasGroup == null || dur <= 0f)
|
||||
{
|
||||
if (_canvasGroup != null) _canvasGroup.alpha = to;
|
||||
yield break;
|
||||
}
|
||||
float t = 0f;
|
||||
while (t < dur)
|
||||
{
|
||||
t += Time.unscaledDeltaTime;
|
||||
_canvasGroup.alpha = Mathf.Lerp(from, to, t / dur);
|
||||
yield return null;
|
||||
}
|
||||
_canvasGroup.alpha = to;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/_Game/Scripts/UI/SubtitleManager.cs.meta
Normal file
11
Assets/_Game/Scripts/UI/SubtitleManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e2dd2823178ffa347970cb5ad9313049
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/_Game/Scripts/UI/Theme.meta
Normal file
8
Assets/_Game/Scripts/UI/Theme.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 321bbb08e4696894bbd9b34e868b223d
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
99
Assets/_Game/Scripts/UI/Theme/UIThemeApplier.cs
Normal file
99
Assets/_Game/Scripts/UI/Theme/UIThemeApplier.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
|
||||
namespace BaseGames.UI.Theme
|
||||
{
|
||||
/// <summary>
|
||||
/// 将 <see cref="UIThemeSO"/> 中的视觉令牌应用到当前 GameObject 及其子节点。
|
||||
///
|
||||
/// 用法:
|
||||
/// 1. 在 Prefab 根节点挂载此组件并指定 <see cref="_theme"/>;
|
||||
/// 2. 在每个需要主题化的子节点添加 <see cref="UIThemeRole"/> 组件指定角色;
|
||||
/// 3. <see cref="Apply"/> 会被 <see cref="OnEnable"/> 自动调用一次,
|
||||
/// 也可在主题切换后手动调用。
|
||||
///
|
||||
/// 性能:使用 <see cref="Component.GetComponentsInChildren{T}(bool)"/> 一次性收集,
|
||||
/// 仅在 Enable / 显式刷新时执行;运行时无每帧成本。
|
||||
/// </summary>
|
||||
[DisallowMultipleComponent]
|
||||
public class UIThemeApplier : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private UIThemeSO _theme;
|
||||
[Tooltip("启用时自动应用一次。运行时切换主题可手动调用 Apply()。")]
|
||||
[SerializeField] private bool _applyOnEnable = true;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (_applyOnEnable) Apply();
|
||||
}
|
||||
|
||||
/// <summary>用当前 <see cref="_theme"/> 覆盖子节点上所有 <see cref="UIThemeRole"/> 标记。</summary>
|
||||
public void Apply()
|
||||
{
|
||||
if (_theme == null) return;
|
||||
|
||||
var roles = GetComponentsInChildren<UIThemeRole>(includeInactive: true);
|
||||
for (int i = 0; i < roles.Length; i++)
|
||||
ApplyRole(roles[i], _theme);
|
||||
}
|
||||
|
||||
/// <summary>运行时切换主题。</summary>
|
||||
public void SetTheme(UIThemeSO theme)
|
||||
{
|
||||
_theme = theme;
|
||||
Apply();
|
||||
}
|
||||
|
||||
private static void ApplyRole(UIThemeRole role, UIThemeSO theme)
|
||||
{
|
||||
if (role == null) return;
|
||||
switch (role.Kind)
|
||||
{
|
||||
case UIThemeRoleKind.Graphic_Primary: SetGraphicColor(role, theme.Primary); break;
|
||||
case UIThemeRoleKind.Graphic_Secondary: SetGraphicColor(role, theme.Secondary); break;
|
||||
case UIThemeRoleKind.Graphic_Accent: SetGraphicColor(role, theme.Accent); break;
|
||||
case UIThemeRoleKind.Graphic_Background: SetGraphicColor(role, theme.Background); break;
|
||||
case UIThemeRoleKind.Graphic_Success: SetGraphicColor(role, theme.Success); break;
|
||||
case UIThemeRoleKind.Graphic_Warning: SetGraphicColor(role, theme.Warning); break;
|
||||
case UIThemeRoleKind.Graphic_Danger: SetGraphicColor(role, theme.Danger); break;
|
||||
|
||||
case UIThemeRoleKind.Text_Primary: SetTextStyle(role, theme.TextPrimary, theme.BodyFont, theme.BodyFontSize); break;
|
||||
case UIThemeRoleKind.Text_Secondary: SetTextStyle(role, theme.TextSecondary, theme.BodyFont, theme.SmallFontSize); break;
|
||||
case UIThemeRoleKind.Text_Header: SetTextStyle(role, theme.TextPrimary, theme.HeaderFont, theme.HeaderFontSize); break;
|
||||
case UIThemeRoleKind.Text_Disabled: SetTextStyle(role, theme.TextDisabled, theme.BodyFont, theme.BodyFontSize); break;
|
||||
|
||||
case UIThemeRoleKind.Button: SetButtonColors(role, theme); break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetGraphicColor(UIThemeRole role, Color c)
|
||||
{
|
||||
var g = role.GetComponent<Graphic>();
|
||||
if (g != null) g.color = c;
|
||||
}
|
||||
|
||||
private static void SetTextStyle(UIThemeRole role, Color c, TMP_FontAsset font, float size)
|
||||
{
|
||||
var tmp = role.GetComponent<TMP_Text>();
|
||||
if (tmp != null)
|
||||
{
|
||||
tmp.color = c;
|
||||
if (font != null) tmp.font = font;
|
||||
if (role.OverrideFontSize) tmp.fontSize = size;
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetButtonColors(UIThemeRole role, UIThemeSO theme)
|
||||
{
|
||||
var btn = role.GetComponent<Button>();
|
||||
if (btn == null) return;
|
||||
var cb = btn.colors;
|
||||
cb.normalColor = theme.ButtonNormal;
|
||||
cb.highlightedColor = theme.ButtonHighlighted;
|
||||
cb.pressedColor = theme.ButtonPressed;
|
||||
cb.disabledColor = theme.ButtonDisabled;
|
||||
btn.colors = cb;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/_Game/Scripts/UI/Theme/UIThemeApplier.cs.meta
Normal file
11
Assets/_Game/Scripts/UI/Theme/UIThemeApplier.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 50572ad108fd3354da04ea8fba66370b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
39
Assets/_Game/Scripts/UI/Theme/UIThemeRole.cs
Normal file
39
Assets/_Game/Scripts/UI/Theme/UIThemeRole.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace BaseGames.UI.Theme
|
||||
{
|
||||
/// <summary>主题角色种类。控制 <see cref="UIThemeApplier"/> 应用何种令牌到目标组件。</summary>
|
||||
public enum UIThemeRoleKind
|
||||
{
|
||||
// Graphic(Image / RawImage / Panel 等)
|
||||
Graphic_Primary,
|
||||
Graphic_Secondary,
|
||||
Graphic_Accent,
|
||||
Graphic_Background,
|
||||
Graphic_Success,
|
||||
Graphic_Warning,
|
||||
Graphic_Danger,
|
||||
|
||||
// TMP_Text
|
||||
Text_Primary,
|
||||
Text_Secondary,
|
||||
Text_Header,
|
||||
Text_Disabled,
|
||||
|
||||
// Button(应用 ColorBlock)
|
||||
Button,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标记组件:告诉 <see cref="UIThemeApplier"/> 当前节点扮演的视觉角色。
|
||||
/// </summary>
|
||||
[DisallowMultipleComponent]
|
||||
public class UIThemeRole : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private UIThemeRoleKind _kind = UIThemeRoleKind.Text_Primary;
|
||||
[SerializeField] private bool _overrideFontSize = false;
|
||||
|
||||
public UIThemeRoleKind Kind => _kind;
|
||||
public bool OverrideFontSize => _overrideFontSize;
|
||||
}
|
||||
}
|
||||
11
Assets/_Game/Scripts/UI/Theme/UIThemeRole.cs.meta
Normal file
11
Assets/_Game/Scripts/UI/Theme/UIThemeRole.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1fa7f5f3910b010489199ef7ddee773e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
46
Assets/_Game/Scripts/UI/Theme/UIThemeSO.cs
Normal file
46
Assets/_Game/Scripts/UI/Theme/UIThemeSO.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
|
||||
namespace BaseGames.UI.Theme
|
||||
{
|
||||
/// <summary>
|
||||
/// 全局 UI 主题资产(架构 10_UIModule §1 视觉风格统一)。
|
||||
///
|
||||
/// 设计动机:避免每个 Prefab 各自硬编码颜色 / 字体,统一替换或本地化主题成本极低。
|
||||
/// 通过 <see cref="UIThemeApplier"/> 在运行时拉取并应用到子节点。
|
||||
///
|
||||
/// 资产路径建议:Assets/Data/UI/Themes/UI_Theme_Default.asset
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "BaseGames/UI/Theme")]
|
||||
public class UIThemeSO : ScriptableObject
|
||||
{
|
||||
[Header("Palette")]
|
||||
public Color Primary = new Color(0.10f, 0.55f, 0.95f);
|
||||
public Color Secondary = new Color(0.20f, 0.20f, 0.25f);
|
||||
public Color Accent = new Color(1.00f, 0.78f, 0.20f);
|
||||
public Color Background = new Color(0.06f, 0.07f, 0.10f);
|
||||
public Color TextPrimary = Color.white;
|
||||
public Color TextSecondary = new Color(0.75f, 0.78f, 0.82f);
|
||||
public Color TextDisabled = new Color(0.40f, 0.42f, 0.45f);
|
||||
public Color Success = new Color(0.30f, 0.85f, 0.45f);
|
||||
public Color Warning = new Color(0.95f, 0.70f, 0.10f);
|
||||
public Color Danger = new Color(0.95f, 0.30f, 0.30f);
|
||||
|
||||
[Header("Typography")]
|
||||
public TMP_FontAsset HeaderFont;
|
||||
public TMP_FontAsset BodyFont;
|
||||
[Min(8)] public float HeaderFontSize = 36f;
|
||||
[Min(8)] public float BodyFontSize = 20f;
|
||||
[Min(8)] public float SmallFontSize = 14f;
|
||||
|
||||
[Header("Button States")]
|
||||
public Color ButtonNormal = new Color(0.10f, 0.55f, 0.95f);
|
||||
public Color ButtonHighlighted = new Color(0.20f, 0.65f, 1.00f);
|
||||
public Color ButtonPressed = new Color(0.05f, 0.40f, 0.80f);
|
||||
public Color ButtonDisabled = new Color(0.30f, 0.30f, 0.32f);
|
||||
|
||||
[Header("Audio (可选)")]
|
||||
public AudioClip ClickSound;
|
||||
public AudioClip HoverSound;
|
||||
}
|
||||
}
|
||||
11
Assets/_Game/Scripts/UI/Theme/UIThemeSO.cs.meta
Normal file
11
Assets/_Game/Scripts/UI/Theme/UIThemeSO.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e1d97086a1337cc47a5d2ee9c85a944d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -62,17 +62,7 @@ namespace BaseGames.UI
|
||||
}
|
||||
|
||||
private IEnumerator FadeTo(float target)
|
||||
{
|
||||
float start = _cg.alpha;
|
||||
float elapsed = 0f;
|
||||
while (elapsed < _fadeDuration)
|
||||
{
|
||||
_cg.alpha = Mathf.Lerp(start, target, elapsed / _fadeDuration);
|
||||
elapsed += Time.unscaledDeltaTime;
|
||||
yield return null;
|
||||
}
|
||||
_cg.alpha = target;
|
||||
}
|
||||
=> UITween.FadeCanvasGroup(_cg, target, _fadeDuration);
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
using BaseGames.Core;
|
||||
using BaseGames.Core.Events;
|
||||
|
||||
@@ -22,17 +24,23 @@ namespace BaseGames.UI
|
||||
[DefaultExecutionOrder(+50)]
|
||||
public class UIManager : MonoBehaviour, IUIManager
|
||||
{
|
||||
// ── 状态驱动根节点(不进入面板栈,仅根据 GameState 显示/隐藏)────────
|
||||
// ── 状态驱动根节点(不进入面板栈,仅根据 GameState 显示/隐藏)─────────
|
||||
[Header("状态驱动根节点(非面板栈)")]
|
||||
[SerializeField] private GameObject _hudRoot;
|
||||
[SerializeField] private GameObject _deathScreenRoot;
|
||||
|
||||
// ── 面板栈注册表 ──────────────────────────────────────────────────────
|
||||
[Header("面板栈注册表(Inspector 配置,可运行时扩展)")]
|
||||
[Tooltip("将 PanelId 与对应的根 GameObject 绑定。" +
|
||||
"新增面板只需在此添加一行,无需修改 UIManager 代码。")]
|
||||
[Tooltip("将 PanelId 与对应的根 GameObject 绑定。新增面板只需在此添加一行,无需修改 UIManager 代码。")]
|
||||
[SerializeField] private PanelRegistration[] _panels;
|
||||
|
||||
[Header("Addressable 面板(按需异步加载,首次打开时实例化)")]
|
||||
[Tooltip("配置 PanelId → AssetReferenceGameObject;同 _panels 重复定义时,本表优先生效。" +
|
||||
"未在此处也未在 _panels 中注册的 PanelId 调用 OpenPanel 将记录警告。")]
|
||||
[SerializeField] private AddressablePanelRegistration[] _addressablePanels;
|
||||
[Tooltip("Addressable 面板实例化后挂载的父节点;为空时挂在 UIManager.transform。")]
|
||||
[SerializeField] private Transform _addressablePanelParent;
|
||||
|
||||
[Header("Event Channels")]
|
||||
[SerializeField] private GameStateEventChannelSO _onGameStateChanged;
|
||||
[SerializeField] private VoidEventChannelSO _onPauseRequested;
|
||||
@@ -43,11 +51,11 @@ namespace BaseGames.UI
|
||||
[SerializeField] private VoidEventChannelSO _onSpellSelectOpen;
|
||||
|
||||
// ── 面板栈结构 ────────────────────────────────────────────────────────
|
||||
private readonly Stack<GameObject> _panelStack = new();
|
||||
private readonly Stack<GameObject> _panelStack = new();
|
||||
/// <summary>O(1) 成员判断,与 _panelStack 保持同步,替代 Stack.Contains O(n)。</summary>
|
||||
private readonly HashSet<GameObject> _openPanelSet = new();
|
||||
private readonly HashSet<GameObject> _openPanelSet = new();
|
||||
private readonly Dictionary<PanelId, GameObject> _panelRegistry = new();
|
||||
private readonly CompositeDisposable _subs = new();
|
||||
private readonly CompositeDisposable _subs = new();
|
||||
|
||||
// ── 序列化辅助结构 ────────────────────────────────────────────────────
|
||||
[System.Serializable]
|
||||
@@ -59,13 +67,42 @@ namespace BaseGames.UI
|
||||
public GameObject root;
|
||||
}
|
||||
|
||||
// ── 生命周期 ──────────────────────────────────────────────────────────
|
||||
[System.Serializable]
|
||||
private struct AddressablePanelRegistration
|
||||
{
|
||||
[Tooltip("面板标识符。")]
|
||||
public PanelId id;
|
||||
[Tooltip("Addressable 引用;首次 OpenPanel 时异步加载并实例化。")]
|
||||
public AssetReferenceGameObject reference;
|
||||
[Tooltip("关闭后保留实例(仅隐藏,不释放)。开启时常驻内存,避免反复加载抖动;适合频繁打开的面板。")]
|
||||
public bool keepLoadedAfterClose;
|
||||
}
|
||||
|
||||
/// <summary>记录 Addressable 加载状态,避免重复加载与正确释放。</summary>
|
||||
private class AddressablePanelHandle
|
||||
{
|
||||
public AsyncOperationHandle<GameObject> Handle;
|
||||
public GameObject Instance;
|
||||
public bool IsLoading;
|
||||
public bool KeepLoaded;
|
||||
}
|
||||
private readonly Dictionary<PanelId, AddressablePanelHandle> _addressableHandles = new();
|
||||
private readonly Dictionary<PanelId, AssetReferenceGameObject> _addressableRefs = new();
|
||||
private readonly Dictionary<PanelId, bool> _addressableKeep = new();
|
||||
|
||||
// ── 生命周期 ──────────────────────────────────────────────────────────
|
||||
private void Awake()
|
||||
{
|
||||
if (_panels != null)
|
||||
foreach (var p in _panels)
|
||||
if (p.root != null) _panelRegistry[p.id] = p.root;
|
||||
|
||||
if (_addressablePanels != null)
|
||||
foreach (var p in _addressablePanels)
|
||||
{
|
||||
if (p.reference != null) _addressableRefs[p.id] = p.reference;
|
||||
_addressableKeep[p.id] = p.keepLoadedAfterClose;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
@@ -84,21 +121,36 @@ namespace BaseGames.UI
|
||||
{
|
||||
ServiceLocator.Unregister<IUIManager>(this);
|
||||
_subs.Clear();
|
||||
ReleaseAllAddressablePanels();
|
||||
}
|
||||
|
||||
/// <summary>释放所有由 Addressables 加载的面板(防止场景切换泄漏)。</summary>
|
||||
private void ReleaseAllAddressablePanels()
|
||||
{
|
||||
foreach (var kv in _addressableHandles)
|
||||
{
|
||||
var h = kv.Value;
|
||||
if (h == null) continue;
|
||||
if (h.Instance != null && _addressableRefs.TryGetValue(kv.Key, out var aref))
|
||||
{
|
||||
aref.ReleaseInstance(h.Instance);
|
||||
}
|
||||
else if (h.Handle.IsValid())
|
||||
{
|
||||
Addressables.Release(h.Handle);
|
||||
}
|
||||
}
|
||||
_addressableHandles.Clear();
|
||||
}
|
||||
|
||||
// ── 面板注册(运行时动态扩展入口)────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// 运行时注册或覆盖面板绑定(如场景加载后动态添加的面板)。
|
||||
/// Inspector 中已配置的面板无需调用此方法。
|
||||
/// </summary>
|
||||
/// <summary>运行时注册或覆盖面板绑定(如场景加载后动态添加的面板)。</summary>
|
||||
public void RegisterPanel(PanelId id, GameObject root)
|
||||
{
|
||||
if (root != null) _panelRegistry[id] = root;
|
||||
}
|
||||
|
||||
// ── 状态响应 ──────────────────────────────────────────────────────────
|
||||
|
||||
private void HandleGameStateChanged(GameStateId state)
|
||||
{
|
||||
bool showHud = state == GameStates.Gameplay || state == GameStates.BossFight;
|
||||
@@ -118,19 +170,72 @@ namespace BaseGames.UI
|
||||
}
|
||||
|
||||
// ── 面板栈 API ────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>通过 ID 打开已注册的面板。</summary>
|
||||
/// <summary>通过 ID 打开面板:优先同步注册表,其次 Addressable 异步加载。</summary>
|
||||
public void OpenPanel(PanelId id)
|
||||
{
|
||||
if (_panelRegistry.TryGetValue(id, out var panel))
|
||||
{
|
||||
OpenPanel(panel);
|
||||
return;
|
||||
}
|
||||
if (_addressableRefs.ContainsKey(id))
|
||||
{
|
||||
OpenPanelAsync(id);
|
||||
return;
|
||||
}
|
||||
Debug.LogWarning($"[UIManager] PanelId.{id} 未注册(Inspector 中既无 _panels 也无 _addressablePanels)。", this);
|
||||
}
|
||||
|
||||
/// <summary>异步加载并打开 Addressable 面板。首次触发 InstantiateAsync,后续复用。</summary>
|
||||
private void OpenPanelAsync(PanelId id)
|
||||
{
|
||||
if (!_addressableRefs.TryGetValue(id, out var aref) || aref == null)
|
||||
{
|
||||
Debug.LogWarning($"[UIManager] Addressable 面板 {id} 未配置 AssetReference。", this);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_addressableHandles.TryGetValue(id, out var entry))
|
||||
{
|
||||
_addressableKeep.TryGetValue(id, out var keep);
|
||||
entry = new AddressablePanelHandle { IsLoading = true, KeepLoaded = keep };
|
||||
_addressableHandles[id] = entry;
|
||||
}
|
||||
|
||||
// 已加载完成:直接复用实例。
|
||||
if (entry.Instance != null)
|
||||
{
|
||||
_panelRegistry[id] = entry.Instance;
|
||||
OpenPanel(entry.Instance);
|
||||
return;
|
||||
}
|
||||
|
||||
// 加载中:忽略重复请求。
|
||||
if (entry.IsLoading && entry.Handle.IsValid()) return;
|
||||
|
||||
entry.IsLoading = true;
|
||||
var parent = _addressablePanelParent != null ? _addressablePanelParent : transform;
|
||||
entry.Handle = aref.InstantiateAsync(parent, instantiateInWorldSpace: false);
|
||||
entry.Handle.Completed += op =>
|
||||
{
|
||||
entry.IsLoading = false;
|
||||
if (op.Status != AsyncOperationStatus.Succeeded || op.Result == null)
|
||||
{
|
||||
Debug.LogError($"[UIManager] Addressable 面板 {id} 加载失败: {op.OperationException}", this);
|
||||
return;
|
||||
}
|
||||
entry.Instance = op.Result;
|
||||
entry.Instance.SetActive(false); // 与 PanelRegistration 同语义:默认隐藏
|
||||
_panelRegistry[id] = entry.Instance;
|
||||
OpenPanel(entry.Instance);
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>打开指定 GameObject 面板并压栈;已在栈中则忽略(O(1) 判断)。</summary>
|
||||
public void OpenPanel(GameObject panel)
|
||||
{
|
||||
if (panel == null) return;
|
||||
if (!_openPanelSet.Add(panel)) return; // HashSet.Add 返回 false = 已存在
|
||||
if (!_openPanelSet.Add(panel)) return;
|
||||
if (_panelStack.Count > 0) _panelStack.Peek().SetActive(false);
|
||||
panel.SetActive(true);
|
||||
_panelStack.Push(panel);
|
||||
@@ -152,7 +257,6 @@ namespace BaseGames.UI
|
||||
}
|
||||
|
||||
// ── 快捷事件回调 ──────────────────────────────────────────────────────
|
||||
|
||||
private void TogglePause()
|
||||
{
|
||||
if (_panelRegistry.TryGetValue(PanelId.Pause, out var pausePanel)
|
||||
@@ -166,35 +270,53 @@ namespace BaseGames.UI
|
||||
private void OpenCharmPanel() => OpenPanel(PanelId.CharmPanel);
|
||||
private void OpenSpellSelect() => OpenPanel(PanelId.SpellSelect);
|
||||
|
||||
// ── 编辑器工具 (不入构建) ──────────────────────────────────────────────────
|
||||
|
||||
/// <summary>验证面板注册表是否完整、无重复、无空引用。</summary>
|
||||
// ── 编辑器工具 ────────────────────────────────────────────────────────
|
||||
[ContextMenu("验证面板注册表")]
|
||||
private void EditorValidateRegistry()
|
||||
{
|
||||
if (_panels == null || _panels.Length == 0)
|
||||
if ((_panels == null || _panels.Length == 0) && (_addressablePanels == null || _addressablePanels.Length == 0))
|
||||
{
|
||||
Debug.LogWarning("[UIManager] 面板注册表为空!", this);
|
||||
return;
|
||||
}
|
||||
|
||||
var seen = new System.Collections.Generic.HashSet<PanelId>();
|
||||
var seen = new HashSet<PanelId>();
|
||||
bool ok = true;
|
||||
foreach (var p in _panels)
|
||||
if (_panels != null)
|
||||
{
|
||||
if (p.root == null)
|
||||
foreach (var p in _panels)
|
||||
{
|
||||
Debug.LogWarning($"[UIManager] PanelId.{p.id} 的 GameObject 引用为 null!", this);
|
||||
ok = false;
|
||||
if (p.root == null)
|
||||
{
|
||||
Debug.LogWarning($"[UIManager] PanelId.{p.id} 的 GameObject 引用为 null!", this);
|
||||
ok = false;
|
||||
}
|
||||
if (!seen.Add(p.id))
|
||||
{
|
||||
Debug.LogError($"[UIManager] PanelId.{p.id} 在 _panels 中重复!", this);
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
if (!seen.Add(p.id))
|
||||
}
|
||||
if (_addressablePanels != null)
|
||||
{
|
||||
var seenAddr = new HashSet<PanelId>();
|
||||
foreach (var p in _addressablePanels)
|
||||
{
|
||||
Debug.LogError($"[UIManager] PanelId.{p.id} 重复注册!", this);
|
||||
ok = false;
|
||||
if (p.reference == null)
|
||||
{
|
||||
Debug.LogWarning($"[UIManager] Addressable PanelId.{p.id} 的 reference 为 null!", this);
|
||||
ok = false;
|
||||
}
|
||||
if (!seenAddr.Add(p.id))
|
||||
{
|
||||
Debug.LogError($"[UIManager] PanelId.{p.id} 在 _addressablePanels 中重复!", this);
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ok)
|
||||
Debug.Log($"[UIManager] 验证通过 ✔ 已注册 {_panels.Length} 个面板。", this);
|
||||
Debug.Log($"[UIManager] 验证通过 ✔ 同步 {_panels?.Length ?? 0} 个 / Addressable {_addressablePanels?.Length ?? 0} 个。", this);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
@@ -203,15 +325,15 @@ namespace BaseGames.UI
|
||||
#endif
|
||||
|
||||
[ContextMenu("测试:打开 Pause 面板")]
|
||||
private void EditorOpenPause() => OpenPanel(PanelId.Pause);
|
||||
private void EditorOpenPause() => OpenPanel(PanelId.Pause);
|
||||
|
||||
[ContextMenu("测试:打开 Map 面板")]
|
||||
private void EditorOpenMap() => OpenPanel(PanelId.Map);
|
||||
private void EditorOpenMap() => OpenPanel(PanelId.Map);
|
||||
|
||||
[ContextMenu("测试:打开 Shop 面板")]
|
||||
private void EditorOpenShop() => OpenPanel(PanelId.Shop);
|
||||
private void EditorOpenShop() => OpenPanel(PanelId.Shop);
|
||||
|
||||
[ContextMenu("测试:关闭栈顶面板")]
|
||||
private void EditorCloseTop() => CloseTopPanel();
|
||||
private void EditorCloseTop() => CloseTopPanel();
|
||||
}
|
||||
}
|
||||
|
||||
8
Assets/_Game/Scripts/UI/Utility.meta
Normal file
8
Assets/_Game/Scripts/UI/Utility.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f68dca2fd99c2f24699b91734614eb2b
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
66
Assets/_Game/Scripts/UI/Utility/ColorblindApplier.cs
Normal file
66
Assets/_Game/Scripts/UI/Utility/ColorblindApplier.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using UnityEngine;
|
||||
using BaseGames.Core;
|
||||
|
||||
namespace BaseGames.UI.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// 色盲滤镜接入点(架构 10_UIModule §可访问性)。
|
||||
/// 默认实现把 <see cref="ColorblindMode"/> 写入两个全局 Shader 参数:
|
||||
/// _GlobalColorblindMode (int) :0=None, 1=Protanopia, 2=Deuteranopia, 3=Tritanopia
|
||||
/// _GlobalColorblindStrength (float):固定 1.0,预留给后续过渡淡入
|
||||
///
|
||||
/// 项目接入方式:
|
||||
/// - URP:在 RendererFeature / PostProcess 中读取上述全局参数做 LUT 切换或 Daltonization 计算。
|
||||
/// - 内置管线:自定义 Image Effect 读取这两个参数。
|
||||
///
|
||||
/// 不依赖任何后处理包,挂在持久 GameObject 即可(DontDestroyOnLoad)。
|
||||
/// 业务方可实现 <see cref="IColorblindApplier"/> 并通过 ServiceLocator 替换默认行为。
|
||||
/// </summary>
|
||||
[DefaultExecutionOrder(-700)]
|
||||
public class ColorblindApplier : MonoBehaviour, IColorblindApplier
|
||||
{
|
||||
private static readonly int ModeId = Shader.PropertyToID("_GlobalColorblindMode");
|
||||
private static readonly int StrengthId = Shader.PropertyToID("_GlobalColorblindStrength");
|
||||
|
||||
[Tooltip("打开后会写入 Shader 全局变量,但不会强制后处理生效;后处理流程需自行读取。")]
|
||||
[SerializeField] private bool _writeShaderGlobals = true;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
ServiceLocator.RegisterIfAbsent<IColorblindApplier>(this);
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
SettingsManager.SettingsChanged += OnSettingsChanged;
|
||||
// 启动时若已加载,主动同步一次
|
||||
var svc = ServiceLocator.GetOrDefault<ISettingsService>();
|
||||
if (svc?.Current != null) ApplyMode(svc.Current.ColorblindMode);
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
SettingsManager.SettingsChanged -= OnSettingsChanged;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
ServiceLocator.Unregister<IColorblindApplier>(this);
|
||||
}
|
||||
|
||||
private void OnSettingsChanged(GlobalSettingsData data) => ApplyMode(data.ColorblindMode);
|
||||
|
||||
public void ApplyMode(ColorblindMode mode)
|
||||
{
|
||||
if (!_writeShaderGlobals) return;
|
||||
Shader.SetGlobalInt(ModeId, (int)mode);
|
||||
Shader.SetGlobalFloat(StrengthId, mode == ColorblindMode.None ? 0f : 1f);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>色盲滤镜接入接口;由 ColorblindApplier 默认实现。</summary>
|
||||
public interface IColorblindApplier
|
||||
{
|
||||
void ApplyMode(ColorblindMode mode);
|
||||
}
|
||||
}
|
||||
11
Assets/_Game/Scripts/UI/Utility/ColorblindApplier.cs.meta
Normal file
11
Assets/_Game/Scripts/UI/Utility/ColorblindApplier.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7c8f1d9c11cc5cb43bdf3177b8028532
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
67
Assets/_Game/Scripts/UI/Utility/UIScaleApplier.cs
Normal file
67
Assets/_Game/Scripts/UI/Utility/UIScaleApplier.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using BaseGames.Core;
|
||||
|
||||
namespace BaseGames.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// 将 <see cref="ISettingsService"/>.<c>Current.UIScale</c> 应用到挂载的
|
||||
/// <see cref="CanvasScaler"/>。挂在每个根 Canvas 上即可。
|
||||
///
|
||||
/// 实现要点:
|
||||
/// · 记录 Inspector 初始的 <c>scaleFactor</c> / <c>referenceResolution</c> 作为基准值,
|
||||
/// 避免反复缩放导致的累积漂移;
|
||||
/// · 订阅 <see cref="SettingsManager.SettingsChanged"/> 在玩家调整后即时刷新;
|
||||
/// · 兼容 <see cref="CanvasScaler.ScaleMode.ScaleWithScreenSize"/> 与
|
||||
/// <see cref="CanvasScaler.ScaleMode.ConstantPixelSize"/> 两种模式。
|
||||
/// </summary>
|
||||
[DisallowMultipleComponent]
|
||||
[RequireComponent(typeof(CanvasScaler))]
|
||||
public class UIScaleApplier : MonoBehaviour
|
||||
{
|
||||
private CanvasScaler _scaler;
|
||||
private float _baseScaleFactor;
|
||||
private Vector2 _baseReferenceResolution;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_scaler = GetComponent<CanvasScaler>();
|
||||
_baseScaleFactor = _scaler.scaleFactor;
|
||||
_baseReferenceResolution = _scaler.referenceResolution;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
SettingsManager.SettingsChanged += OnSettingsChanged;
|
||||
Apply();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
SettingsManager.SettingsChanged -= OnSettingsChanged;
|
||||
}
|
||||
|
||||
private void OnSettingsChanged(GlobalSettingsData _) => Apply();
|
||||
|
||||
private void Apply()
|
||||
{
|
||||
var svc = ServiceLocator.GetOrDefault<ISettingsService>();
|
||||
float ui = svc?.Current?.UIScale ?? 1f;
|
||||
if (ui <= 0f) ui = 1f;
|
||||
|
||||
switch (_scaler.uiScaleMode)
|
||||
{
|
||||
case CanvasScaler.ScaleMode.ConstantPixelSize:
|
||||
_scaler.scaleFactor = _baseScaleFactor * ui;
|
||||
break;
|
||||
case CanvasScaler.ScaleMode.ScaleWithScreenSize:
|
||||
// 缩小参考分辨率 → 等价于放大 UI。
|
||||
_scaler.referenceResolution = _baseReferenceResolution / ui;
|
||||
break;
|
||||
default:
|
||||
_scaler.scaleFactor = _baseScaleFactor * ui;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/_Game/Scripts/UI/Utility/UIScaleApplier.cs.meta
Normal file
11
Assets/_Game/Scripts/UI/Utility/UIScaleApplier.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9eae5e71f8a9b5b4582bfa1f0b41ac52
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
131
Assets/_Game/Scripts/UI/Utility/UITween.cs
Normal file
131
Assets/_Game/Scripts/UI/Utility/UITween.cs
Normal file
@@ -0,0 +1,131 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace BaseGames.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// UI 协程补间静态库。
|
||||
///
|
||||
/// 设计动机:消除 <see cref="BossHPBar"/>、<see cref="ToastNotification"/>、
|
||||
/// <see cref="FloatingDamageText"/> 等组件中重复出现的 Lerp + WaitForEndOfFrame 样板,
|
||||
/// 集中维护时间步与回调约定。所有协程默认使用 <see cref="Time.unscaledDeltaTime"/>,
|
||||
/// 以便 UI 动画在游戏暂停(<see cref="Time.timeScale"/> = 0)时仍能播放。
|
||||
///
|
||||
/// 性能特征:
|
||||
/// · 无堆分配(除协程对象本身);
|
||||
/// · 提前返回保护无效目标;
|
||||
/// · <paramref name="duration"/> <= 0 时立即吸附到终态并退出(一帧 yield 用于保持
|
||||
/// 与正常协程一致的栈语义)。
|
||||
/// </summary>
|
||||
public static class UITween
|
||||
{
|
||||
// ── 位置 ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>将 RectTransform 的 anchoredPosition 平滑过渡到目标值。</summary>
|
||||
public static IEnumerator MoveAnchored(RectTransform rect,
|
||||
Vector2 target,
|
||||
float duration,
|
||||
bool unscaled = true)
|
||||
{
|
||||
if (rect == null) yield break;
|
||||
if (duration <= 0f)
|
||||
{
|
||||
rect.anchoredPosition = target;
|
||||
yield break;
|
||||
}
|
||||
|
||||
Vector2 start = rect.anchoredPosition;
|
||||
float t = 0f;
|
||||
while (t < duration)
|
||||
{
|
||||
rect.anchoredPosition = Vector2.Lerp(start, target, t / duration);
|
||||
t += unscaled ? Time.unscaledDeltaTime : Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
rect.anchoredPosition = target;
|
||||
}
|
||||
|
||||
// ── 透明度 ───────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>将 CanvasGroup.alpha 平滑过渡到目标值。</summary>
|
||||
public static IEnumerator FadeCanvasGroup(CanvasGroup cg,
|
||||
float target,
|
||||
float duration,
|
||||
bool unscaled = true)
|
||||
{
|
||||
if (cg == null) yield break;
|
||||
if (duration <= 0f)
|
||||
{
|
||||
cg.alpha = target;
|
||||
yield break;
|
||||
}
|
||||
|
||||
float start = cg.alpha;
|
||||
float t = 0f;
|
||||
while (t < duration)
|
||||
{
|
||||
cg.alpha = Mathf.Lerp(start, target, t / duration);
|
||||
t += unscaled ? Time.unscaledDeltaTime : Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
cg.alpha = target;
|
||||
}
|
||||
|
||||
/// <summary>将 Graphic(Image / TMP_Text 等)的颜色 Alpha 通道平滑过渡到目标值。</summary>
|
||||
public static IEnumerator FadeGraphic(Graphic graphic,
|
||||
float targetAlpha,
|
||||
float duration,
|
||||
bool unscaled = true)
|
||||
{
|
||||
if (graphic == null) yield break;
|
||||
if (duration <= 0f)
|
||||
{
|
||||
var c = graphic.color;
|
||||
c.a = targetAlpha;
|
||||
graphic.color = c;
|
||||
yield break;
|
||||
}
|
||||
|
||||
float startAlpha = graphic.color.a;
|
||||
float t = 0f;
|
||||
while (t < duration)
|
||||
{
|
||||
var c = graphic.color;
|
||||
c.a = Mathf.Lerp(startAlpha, targetAlpha, t / duration);
|
||||
graphic.color = c;
|
||||
t += unscaled ? Time.unscaledDeltaTime : Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
var fc = graphic.color;
|
||||
fc.a = targetAlpha;
|
||||
graphic.color = fc;
|
||||
}
|
||||
|
||||
// ── 缩放 ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>将 Transform 的 localScale 等比平滑过渡到目标值。</summary>
|
||||
public static IEnumerator Scale(Transform tr,
|
||||
Vector3 target,
|
||||
float duration,
|
||||
bool unscaled = true)
|
||||
{
|
||||
if (tr == null) yield break;
|
||||
if (duration <= 0f)
|
||||
{
|
||||
tr.localScale = target;
|
||||
yield break;
|
||||
}
|
||||
|
||||
Vector3 start = tr.localScale;
|
||||
float t = 0f;
|
||||
while (t < duration)
|
||||
{
|
||||
tr.localScale = Vector3.Lerp(start, target, t / duration);
|
||||
t += unscaled ? Time.unscaledDeltaTime : Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
tr.localScale = target;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/_Game/Scripts/UI/Utility/UITween.cs.meta
Normal file
11
Assets/_Game/Scripts/UI/Utility/UITween.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 96fd9d373d6d2784c8d04d8e56d0bb3e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user