204 lines
4.7 KiB
C#
204 lines
4.7 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;
|
||
|
||
[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;
|
||
// }
|
||
//}
|
||
}
|
||
|
||
private void Awake()
|
||
{
|
||
_hasAwake = true;
|
||
CachedObj = gameObject;
|
||
BindWidget();
|
||
RegisterEvent();
|
||
|
||
ApplyShowType();
|
||
Refresh();
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
UnRegisterEvent();
|
||
}
|
||
|
||
//private void Show()
|
||
//{
|
||
// if (!_hasAwake)
|
||
// return;
|
||
// GetNode();
|
||
// ApplyShowType();
|
||
// Refresh();
|
||
//}
|
||
|
||
public void SetNode(int nodeID, int msgCount)
|
||
{
|
||
this._nodeID = nodeID;
|
||
showTag = msgCount;
|
||
|
||
ApplyShowType();
|
||
Refresh();
|
||
}
|
||
|
||
|
||
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<int[]>(EventManager.EventName.RedPointDataChange, OnRedPointDataChange);
|
||
}
|
||
|
||
private void UnRegisterEvent()
|
||
{
|
||
EventManager.Instance.Unregister<int[]>(EventManager.EventName.RedPointDataChange, OnRedPointDataChange);
|
||
}
|
||
|
||
private void OnRedPointDataChange(int[] nodeMsg)
|
||
{
|
||
if (nodeMsg[0] == NodeID)
|
||
{
|
||
showTag = nodeMsg[1];
|
||
Refresh();
|
||
}
|
||
}
|
||
|
||
private void Refresh()
|
||
{
|
||
if (NodeID != NODE_DEFAULT_ID && showTag > 0)
|
||
{
|
||
//var count = _node.MessageCount;
|
||
CachedObj.transform.localScale = Vector3.one;//用scale替代setActive,避免在循环列表内单个被隐藏而全部错误显示的问题
|
||
_count.text = showTag.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
|
||
}
|