//事件的类型
public enum EventType
{
//例如
ShowMainPanel,
AddScore,
}
public delegate void Callback_Handle(object _data);
private Dictionary<EventType, Callback_Handle> _EventList =new Dictionary<EventType, Callback_Handle>();
//添加普通事件观察者
public void AddEventListener(LogicEventType _eventType, Callback_Handle _callback)
{
if (_EventList.ContainsKey(_eventType))
{
_EventList[_eventType] += _callback;
}
else
{
_EventList.Add(_eventType, _callback);
}
}
//删除普通事件观察者
public void RemoveEventListener(EventType _eventType, Callback_Handle _callback)
{
if (_EventList.ContainsKey(_eventType))
{
_EventList[_eventType] -= _callback;
if (_EventList[_eventType] == null)
{
_EventList.Remove(_eventType);
}
}
}
//推送消息
public void PostEvent(EventType _eventType, object data = null)
{
if (_EventList.ContainsKey(_eventType))
{
try
{
_EventList[_eventType](data);
}
catch (Exception e)
{
Debug.LogError("事件通知时出错:" + e.ToString());
}
}
}