208 lines
4.9 KiB
C#
208 lines
4.9 KiB
C#
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")]
|
||
[InfoBox("如需动态设置红点,请预设为 -1,确保节点在未绑定红点时不显示")]
|
||
[SerializeField]
|
||
private int _nodeID = NODE_DEFAULT_ID;
|
||
|
||
[SerializeField]
|
||
private int ParentID;
|
||
|
||
[LabelText("消息数(为0时红点隐藏)")]
|
||
[SerializeField]
|
||
private int showTag = -1;
|
||
[SerializeField]
|
||
private int childCnt = 0;
|
||
|
||
[OnValueChanged("OnShowTypeChanged")]
|
||
[LabelText("展示类型")]
|
||
[SerializeField]
|
||
private ERedPointShowType _showType = ERedPointShowType.IconAndCount;
|
||
|
||
public GameObject CachedObj { get; private set; }
|
||
|
||
private bool _hasAwake;
|
||
|
||
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()
|
||
{
|
||
_hasAwake = true;
|
||
CachedObj = gameObject;
|
||
BindWidget();
|
||
RegisterEvent();
|
||
Show();
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
UnRegisterEvent();
|
||
}
|
||
|
||
private void Show()
|
||
{
|
||
if(!_hasAwake)
|
||
return;
|
||
GetNode();
|
||
ApplyShowType();
|
||
Refresh();
|
||
}
|
||
|
||
private void GetNode()
|
||
{
|
||
if (_nodeID == NODE_DEFAULT_ID)
|
||
{
|
||
_node = null;
|
||
return;
|
||
}
|
||
_node = RedPointManager.Instance.GetNodeByID(_nodeID);
|
||
ParentID = _node?.parent?.ID ?? 0;
|
||
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;
|
||
}
|
||
|
||
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,避免在循环列表内单个被隐藏而全部错误显示的问题
|
||
_count.text = count.ToString();
|
||
|
||
showTag = _node.MessageCount;
|
||
childCnt = _node.ChildCount;
|
||
}
|
||
else
|
||
{
|
||
CachedObj.transform.localScale = Vector3.zero;
|
||
}
|
||
}
|
||
|
||
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
|
||
}
|