UI相关优化补充
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
// ── 回收 ─────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user