using UnityEngine; using Sirenix.OdinInspector; using UnityEngine.UI; using TMPro; using Framework; using Debug = DebugUtil; public enum ERedPointShowType { [LabelText("隐藏")] None = 0, [LabelText("红点")] Icon = 1, [LabelText("数字")] Count = 2, [LabelText("红点加数字")] IconAndCount = 3, } public class RedPointUIController : MonoBehaviour { const string ICON_PATH = "icon"; const string TEXT_PATH = "count"; const int NODE_DEFAULT_ID = -1; private RedPointNode _node; private Image _icon; private TMP_Text _count; [OnValueChanged("OnNodeIDChanged")] [LabelText("节点ID")] [SerializeField] private int _nodeID = NODE_DEFAULT_ID; [OnValueChanged("OnShowTypeChanged")] [LabelText("展示类型")] [SerializeField] private ERedPointShowType _showType = ERedPointShowType.IconAndCount; public GameObject CachedObj { get; private set; } public ERedPointShowType ShowType { get => _showType; set { if (_showType != value) { _showType = value; ApplyShowType(); } } } public int NodeID { get => _nodeID; set { if (_nodeID != value) { _nodeID = value; Show(); } } } private void Awake() { CachedObj = gameObject; BindWidget(); RegisterEvent(); Show(); } private void OnDestroy() { UnRegisterEvent(); } private void Show() { GetNode(); ApplyShowType(); Refresh(); } private void GetNode() { if (_nodeID == NODE_DEFAULT_ID) { _node = null; return; } _node = RedPointManager.Instance.GetNodeByID(_nodeID); if (_node == null) { Debug.LogError($"找不到RedPointNode!id:{_nodeID}"); } } private void BindWidget() { _icon = transform.Find(ICON_PATH).GetComponent(); if (_icon == null) Debug.LogError("找不到_icon"); _count = transform.Find(TEXT_PATH).GetComponent(); if (_count == null) Debug.LogError("找不到_count"); } public void ResetToDefault() { NodeID = NODE_DEFAULT_ID; } private void RegisterEvent() { EventManager.Instance.Register(EventManager.EventName.RedPointDataChange, OnRedPointDataChange); } private void UnRegisterEvent() { EventManager.Instance.Unregister(EventManager.EventName.RedPointDataChange, OnRedPointDataChange); } private void OnRedPointDataChange(RedPointNode node) { if (node == _node) { Refresh(); } } private void Refresh() { if (_node != null && _node.Valid) { var count = _node.MessageCount; CachedObj.SetActive(count > 0); _count.text = count.ToString(); } else { CachedObj.SetActive(false); } } private void ApplyShowType() { switch (_showType) { case ERedPointShowType.None: _icon.gameObject.SetActive(false); _count.gameObject.SetActive(false); break; case ERedPointShowType.Icon: _icon.gameObject.SetActive(true); _count.gameObject.SetActive(false); break; case ERedPointShowType.Count: _icon.gameObject.SetActive(false); _count.gameObject.SetActive(true); break; case ERedPointShowType.IconAndCount: _icon.gameObject.SetActive(true); _count.gameObject.SetActive(true); break; default: break; } } #region GUI private void OnShowTypeChanged() { if (Application.isPlaying) ApplyShowType(); } private void OnNodeIDChanged() { if (Application.isPlaying) Show(); } #endregion }