Unity的事件管理介绍
1、用于管理事件间的监听和分发
2、减少脚本间的直接关联
3、方便脚本的独立性管理
通讯数据 EventDate
定义接受的数据类型
public delegate void EventDelegate(EventDate date);
public class EventDate
{
public EventDate(uint varID, object vardate)
{
mEventID = varID;
mEventDate = vardate;
}
private uint mEventID;
private object mEventDate;
public uint EventId { get { return mEventID; } }
public object Date { get { return mEventDate; } }
}
事件管理接口 IEventComponent
限制事件管理的访问接口
public interface IEventComponent
{
/// <summary>
/// 发送事件
/// </summary>
/// <param name="eventID">事件id</param>
/// <param name="date">数据</param>
void Fire(uint eventID, object date);
/// <summary>
/// 获取数量监听数量
/// </summary>
/// <param name="eventID">事件id</param>
/// <returns>事件数量</returns>
int Count(uint eventID);
/// <summary>
/// 检测事件是否存在
/// </summary>
/// <param name="eventID">事件id</param>
/// <param name="eventCallback">事件回调</param>
/// <returns>事件监听的存在与否</returns>
bool Check(uint eventID, EventDelegate eventCallback);
/// <summary>
/// 注册事件
/// </summary>
/// <param name="eventID">事件id</param>
/// <param name="eventCallback">事件回调</param>
void Subscribe(uint eventID, EventDelegate eventCallback);
/// <summary>
/// 注销事件
/// </summary>
/// <param name="eventID">事件id</param>
/// <param name="eventCallback">事件回调</param>
void Unsubscribe(uint eventID, EventDelegate eventCallback);
}
事件管理 EventComponent
事件的统一化管理
管理基类: FrameworkComponent
public class EventComponent : FrameworkComponent, IEventComponent
{
private Dictionary<uint, List<EventDelegate>> _allEvent = new Dictionary<uint, List<EventDelegate>>();
public void Fire(uint eventID, object data)
{
if (!_allEvent.ContainsKey(eventID))
{
Debug.LogWarning($"eventid {eventID} is not listen");
}
else
{
if (_allEvent[eventID].Count == 0)
{
Debug.LogWarning($"eventid {eventID} is not listen");
}
else
{
EventDate tempEvent = new EventDate(eventID, data);
var array = _allEvent[eventID];
for (int i = 0; i < array.Count; i++)
{
array[i].Invoke(tempEvent);
}
}
}
}
public bool Check(uint eventID, EventDelegate eventCallback)
{
if (_allEvent.ContainsKey(eventID))
{
if (_allEvent[eventID].Contains(eventCallback))
{
return true;
}
}
return false;
}
public int Count(uint eventID)
{
if (_allEvent.ContainsKey(eventID))
{
return _allEvent[eventID].Count;
}
return 0;
}
public void Subscribe(uint eventID, EventDelegate eventCallback)
{
if (_allEvent.ContainsKey(eventID))
{
if (_allEvent[eventID].Contains(eventCallback))
{
Debug.LogWarning($"eventid :{eventID},method:{eventCallback.Method.Name} subscribe repeat ");
}
else
{
_allEvent[eventID].Add(eventCallback);
}
}
else
{
List<EventDelegate> delegates = new List<EventDelegate>();
delegates.Add(eventCallback);
_allEvent.Add(eventID, delegates);
}
}
public void Unsubscribe(uint eventID, EventDelegate eventCallback)
{
if (_allEvent.ContainsKey(eventID))
{
if (_allEvent[eventID].Contains(eventCallback))
{
_allEvent[eventID].Remove(eventCallback);
}
else
{
Debug.LogWarning($"Method :{eventCallback.Method.Name} is not subscribe event !");
}
}
else
{
Debug.LogWarning($"Event {eventID} is not subscribe event !");
}
}
}