NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/GameWrapper/RedPointUIController.cs

207 lines
4.7 KiB
C#
Raw Normal View History

2023-10-19 15:00:19 +08:00
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;
2025-04-28 17:19:22 +08:00
//private RedPointNode _node;
2023-10-19 15:00:19 +08:00
private Image _icon;
private TMP_Text _count;
[LabelText("节点ID")]
2024-08-15 18:59:14 +08:00
[InfoBox("如需动态设置红点,请预设为 -1确保节点在未绑定红点时不显示")]
2023-10-19 15:00:19 +08:00
[SerializeField]
private int _nodeID = NODE_DEFAULT_ID;
2025-04-28 17:19:22 +08:00
//[SerializeField]
//private int ParentID;
[LabelText("消息数(为0时红点隐藏)")]
[SerializeField]
private int showTag = -1;
2025-04-28 17:19:22 +08:00
//[SerializeField]
//private int childCnt = 0;
2023-10-19 15:00:19 +08:00
[OnValueChanged("OnShowTypeChanged")]
[LabelText("展示类型")]
[SerializeField]
private ERedPointShowType _showType = ERedPointShowType.IconAndCount;
public GameObject CachedObj { get; private set; }
2024-01-18 16:02:45 +08:00
private bool _hasAwake;
2025-04-28 17:19:22 +08:00
2023-10-19 15:00:19 +08:00
public ERedPointShowType ShowType
{
get => _showType;
set
{
if (_showType != value)
{
_showType = value;
ApplyShowType();
}
}
}
public int NodeID
{
get => _nodeID;
2025-04-28 17:19:22 +08:00
//set
//{
// if (_nodeID != value)
// {
// _nodeID = value;
// }
//}
2023-10-19 15:00:19 +08:00
}
private void Awake()
{
2024-01-18 16:02:45 +08:00
_hasAwake = true;
2023-10-19 15:00:19 +08:00
CachedObj = gameObject;
BindWidget();
RegisterEvent();
2025-04-28 17:24:42 +08:00
ApplyShowType();
Refresh();
2023-10-19 15:00:19 +08:00
}
private void OnDestroy()
{
UnRegisterEvent();
}
2025-04-28 17:19:22 +08:00
//private void Show()
//{
// if (!_hasAwake)
// return;
// GetNode();
// ApplyShowType();
// Refresh();
//}
public void SetNode(int nodeID, int msgCount)
2023-10-19 15:00:19 +08:00
{
2025-04-28 17:19:22 +08:00
this._nodeID = nodeID;
showTag = msgCount;
2025-05-06 12:56:18 +08:00
if (_hasAwake)
{
ApplyShowType();
Refresh();
}
2023-10-19 15:00:19 +08:00
}
private void BindWidget()
{
_icon = transform.Find(ICON_PATH).GetComponent<Image>();
if (_icon == null)
Debug.LogError("找不到_icon");
_count = transform.Find(TEXT_PATH).GetComponent<TMP_Text>();
if (_count == null)
Debug.LogError("找不到_count");
}
public void ResetToDefault()
{
2025-04-28 17:19:22 +08:00
_nodeID = NODE_DEFAULT_ID;
}
2025-04-28 17:19:22 +08:00
2023-10-19 15:00:19 +08:00
private void RegisterEvent()
{
2025-04-28 17:19:22 +08:00
EventManager.Instance.Register<int[]>(EventManager.EventName.RedPointDataChange, OnRedPointDataChange);
2023-10-19 15:00:19 +08:00
}
private void UnRegisterEvent()
{
2025-04-28 17:19:22 +08:00
EventManager.Instance.Unregister<int[]>(EventManager.EventName.RedPointDataChange, OnRedPointDataChange);
2023-10-19 15:00:19 +08:00
}
2025-04-28 17:19:22 +08:00
private void OnRedPointDataChange(int[] nodeMsg)
2023-10-19 15:00:19 +08:00
{
2025-04-28 17:19:22 +08:00
if (nodeMsg[0] == NodeID)
2023-10-19 15:00:19 +08:00
{
2025-04-28 17:19:22 +08:00
showTag = nodeMsg[1];
2023-10-19 15:00:19 +08:00
Refresh();
}
}
private void Refresh()
{
2025-04-28 17:19:22 +08:00
if (NodeID != NODE_DEFAULT_ID && showTag > 0)
2023-10-19 15:00:19 +08:00
{
2025-04-28 17:19:22 +08:00
//var count = _node.MessageCount;
CachedObj.transform.localScale = Vector3.one;//用scale替代setActive避免在循环列表内单个被隐藏而全部错误显示的问题
_count.text = showTag.ToString();
2025-04-28 17:19:22 +08:00
//showTag = _node.MessageCount;
//childCnt = _node.ChildCount;
2023-10-19 15:00:19 +08:00
}
else
{
CachedObj.transform.localScale = Vector3.zero;
2023-10-19 15:00:19 +08:00
}
}
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();
}
2025-04-28 17:19:22 +08:00
//private void OnNodeIDChanged()
//{
// if (Application.isPlaying)
// Show();
//}
2023-10-19 15:00:19 +08:00
#endregion
}