NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/RedPoint/RedPointNode.cs

88 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Framework;
using System;
using System.Collections.Generic;
using static Framework.EventManager;
public abstract class RedPointNode
{
private bool _isDisposed;
protected List<RedPointNode> _children = new();
//该红点节点需要注册的事件当事件发生时通知UI刷新数据
protected abstract EventName[] eventsToRegister { get; }
//消息数目要重载,去算各自系统对应的消息数
public abstract int MessageCount { get; }
public int ID { get; private set; }
public RedPointNode parent { get; set; }
public bool Valid => !_isDisposed;
protected RedPointNode(int id)
{
ID = id;
}
public void AddChild(RedPointNode child)
{
_children.Add(child);
}
public void RemoveChild(RedPointNode child)
{
_children.Remove(child);
}
public void Init()
{
RegisterEvents();
}
public void Dispose()
{
UnRegisterEvents();
_children = null;
parent = null;
_isDisposed = true;
}
//如果节点有特殊信号处理的,重写该函数
protected virtual void RegisterEvents()
{
if (eventsToRegister != null)
{
foreach (var eveName in eventsToRegister)
{
EventManager.Instance.Register(eveName, SendChange);
}
}
}
//如果节点有特殊信号处理的,重写该函数
protected virtual void UnRegisterEvents()
{
if (eventsToRegister != null)
{
foreach (var eveName in eventsToRegister)
{
EventManager.Instance.Unregister(eveName, SendChange);
}
}
}
public void ForeachChild(Action<RedPointNode> func)
{
for (int i = _children.Count - 1; i >= 0; i--)
{
func(_children[i]);
}
}
public void SendChange()
{
EventManager.Instance.Send(EventName.RedPointDataChange, this);
if (parent != null)
{
parent.SendChange();
}
}
}