using System; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace Continentis.MainGame.UI { public partial class HUD_BaseIcon : UIElementBase, IPointerEnterHandler, IPointerExitHandler { public Image icon; public List textList; public TMP_Text iconText => textList[0]; public virtual void UpdateIcon() { } public virtual void OnPointerEnter(PointerEventData eventData) { } public virtual void OnPointerExit(PointerEventData eventData) { } } public partial class HUD_BaseIcon { public void SetText(int textIndex, Func getText) { if (textIndex < 0 || textIndex >= textList.Count) { Debug.LogWarning($"Text index {textIndex} is out of range."); return; } TMP_Text text = textList[textIndex]; if (text == null) { Debug.LogWarning($"Text component at index {textIndex} is null."); // 如果这里触发了,说明上层逻辑在尝试操作一个已经死亡的 UI 对象 return; } if (getText != null) { text.gameObject.SetActive(true); text.text = getText(); } else { text.gameObject.SetActive(false); } } } }