NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/RedPoint/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;
private RedPointNode _node;
private Image _icon;
private TMP_Text _count;
[OnValueChanged("OnNodeIDChanged")]
[LabelText("节点ID")]
[SerializeField]
private int _nodeID = NODE_DEFAULT_ID;
[SerializeField]
private int ParentID;
[LabelText("消息数(为0时红点隐藏)")]
[SerializeField]
private int showTag = -1;
[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;
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;
set
{
if (_nodeID != value)
{
_nodeID = value;
Show();
}
}
}
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();
Show();
}
private void OnDestroy()
{
UnRegisterEvent();
}
private void Show()
{
2024-01-18 16:02:45 +08:00
if(!_hasAwake)
return;
2023-10-19 15:00:19 +08:00
GetNode();
ApplyShowType();
Refresh();
}
private void GetNode()
{
if (_nodeID == NODE_DEFAULT_ID)
{
_node = null;
2023-10-19 15:00:19 +08:00
return;
}
2023-10-19 15:00:19 +08:00
_node = RedPointManager.Instance.GetNodeByID(_nodeID);
ParentID = _node?.parent?.ID ?? 0;
2023-10-19 15:00:19 +08:00
if (_node == null)
{
Debug.LogError($"找不到RedPointNode!id:{_nodeID}");
}
}
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()
{
NodeID = NODE_DEFAULT_ID;
}
2023-10-19 15:00:19 +08:00
private void RegisterEvent()
{
EventManager.Instance.Register<RedPointNode>(EventManager.EventName.RedPointDataChange, OnRedPointDataChange);
}
private void UnRegisterEvent()
{
EventManager.Instance.Unregister<RedPointNode>(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.transform.localScale = (count > 0) ? Vector3.one : Vector3.zero;//用scale替代setActive避免在循环列表内单个被隐藏而全部错误显示的问题
2023-10-19 15:00:19 +08:00
_count.text = count.ToString();
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();
}
private void OnNodeIDChanged()
{
if (Application.isPlaying)
Show();
}
#endregion
}