using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; public interface IEventInfo { } public class EventInfo : IEventInfo { public UnityAction actions; public EventInfo(UnityAction action) { actions += action; } } public class EventInfo: IEventInfo { public UnityAction actions; public EventInfo(UnityAction action) { actions += action; } } /// /// 事件中心 /// public class EventCenter : BaseManager { private EventCenter() { } /// /// 存储所有事件 及 关心事件的函数 /// private Dictionary eventDic = new Dictionary(); /// /// 存储所有事件 及 关心事件的函数 /// private Dictionary eventEnumDic = new Dictionary(); /// /// 添加无参事件监听 /// /// 事件名称 /// 无参委托函数 public void AddEventListener(Enum_EventType type, UnityAction action) { if (eventEnumDic.ContainsKey(type)) { (eventEnumDic[type] as EventInfo).actions += action; } else { eventEnumDic.Add(type, new EventInfo(action)); } } /// /// 添加有参事件监听 /// /// 参数类型 /// 事件名称 /// 有参委托函数 public void AddEventListener(Enum_EventType type, UnityAction action) { if (eventEnumDic.ContainsKey(type)) { (eventEnumDic[type] as EventInfo).actions += action; } else { eventEnumDic.Add(type, new EventInfo(action)); } } /// /// 移除无参事件监听 /// /// 事件名称 /// 无参委托函数 public void RemoveEventListener(Enum_EventType type, UnityAction action) { if (eventEnumDic.ContainsKey(type)) { (eventEnumDic[type] as EventInfo).actions -= action; } } /// /// 移除有参事件监听 /// /// /// /// public void RemoveEventListener(Enum_EventType type, UnityAction action) { if (eventEnumDic.ContainsKey(type)) { (eventEnumDic[type] as EventInfo).actions -= action; } } /// /// 触发无参事件 /// /// public void EventTrigger(Enum_EventType type) { if (eventEnumDic.ContainsKey(type)) { (eventEnumDic[type] as EventInfo).actions?.Invoke(); } } /// /// 触发有参事件 /// public void EventTrigger(Enum_EventType type, T info) { if (eventEnumDic.ContainsKey(type)) { (eventEnumDic[type] as EventInfo).actions?.Invoke(info); } } /// /// 添加无参事件监听 /// /// 事件名称 /// 无参委托函数 public void AddEventListener(string eventName, UnityAction action) { if (eventDic.ContainsKey(eventName)) { (eventDic[eventName] as EventInfo).actions += action; } else { eventDic.Add(eventName, new EventInfo(action)); } } /// /// 添加有参事件监听 /// /// 参数类型 /// 事件名称 /// 有参委托函数 public void AddEventListener(string eventName, UnityAction action) { if (eventDic.ContainsKey(eventName)) { (eventDic[eventName] as EventInfo).actions += action; } else { eventDic.Add(eventName, new EventInfo(action)); } } /// /// 移除无参事件监听 /// /// 事件名称 /// 无参委托函数 public void RemoveEventListener(string eventName, UnityAction action) { if (eventDic.ContainsKey(eventName)) { (eventDic[eventName] as EventInfo).actions -= action; } } /// /// 移除有参事件监听 /// /// /// /// public void RemoveEventListener(string eventName, UnityAction action) { if (eventDic.ContainsKey(eventName)) { (eventDic[eventName] as EventInfo).actions -= action; } } /// /// 触发无参事件 /// /// public void EventTrigger(string eventName) { if (eventDic.ContainsKey(eventName)) { (eventDic[eventName] as EventInfo).actions?.Invoke(); } } /// /// 触发有参事件 /// public void EventTrigger(string eventName, T info) { if (eventDic.ContainsKey(eventName)) { (eventDic[eventName] as EventInfo).actions?.Invoke(info); } } /// /// 触发有参事件 /// public void EventTrigger(string eventName, T info) { if (eventDic.ContainsKey(eventName)) { (eventDic[eventName] as EventInfo).actions?.Invoke(info); } } /// /// 清空所有事件监听 /// public void Clear() { eventDic.Clear(); eventEnumDic.Clear(); } }