using System.Collections.Generic; using System.Linq; 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 EventInfo : IEventInfo { public UnityAction actions; public EventInfo(UnityAction action) { actions += action; } } /// /// 事件中心 /// public class EventCenter { public EventCenter() { } /// /// 存储所有事件 及 关心事件的函数 /// // private Dictionary eventDic = new Dictionary(); /// /// 存储所有事件 及 关心事件的函数 /// private Dictionary eventEnumDic = new Dictionary(); /// /// 添加事件监听 /// /// 事件字典 /// 事件名称 /// 委托函数 private void AddEventListener(Dictionary dic, T key, UnityAction action) { if (dic.ContainsKey(key)) (dic[key] as EventInfo).actions += action; else dic.Add(key, new EventInfo(action)); } /// ///添加事件监听 /// /// 事件字典 /// 事件名称 /// 委托函数 private void AddEventListener(Dictionary dic, T key, UnityAction action) { if (dic.ContainsKey(key)) (dic[key] as EventInfo).actions += action; else dic.Add(key, new EventInfo(action)); } /// /// 添加事件监听 /// /// 事件字典 /// 事件名称 /// 委托函数 private void AddEventListener(Dictionary dic, T key, UnityAction action) { if (dic.ContainsKey(key)) (dic[key] as EventInfo).actions += action; else dic.Add(key, new EventInfo(action)); } /// /// 移除事件监听 /// /// 事件字典 /// 事件名称 /// 委托函数 private void RemoveEventListener(Dictionary dic, T key, UnityAction action) { if (dic.ContainsKey(key)) (dic[key] as EventInfo).actions -= action; } /// /// 移除事件监听 /// /// 事件字典 /// 事件名称 /// 委托函数 private void RemoveEventListener(Dictionary dic, T key, UnityAction action) { if (dic.ContainsKey(key)) (dic[key] as EventInfo).actions -= action; } /// /// 移除事件监听 /// /// 事件字典 /// 事件名称 /// 委托函数 private void RemoveEventListener(Dictionary dic, T key, UnityAction action) { if (dic.ContainsKey(key)) (dic[key] as EventInfo).actions -= action; } /// /// 触发事件 /// /// 事件字典 /// 事件名称 private void EventTrigger(Dictionary dic, T key) { if (dic.ContainsKey(key)) (dic[key] as EventInfo).actions?.Invoke(); } /// /// 触发事件 /// /// 事件字典 /// 事件名称 private void EventTrigger(Dictionary dic, T key, U info) { if (dic.ContainsKey(key)) (dic[key] as EventInfo).actions?.Invoke(info); } /// /// 触发事件 /// /// 事件字典 /// 事件名称 private void EventTrigger(Dictionary dic, T key, U info, V info2) { if (dic.ContainsKey(key)) (dic[key] as EventInfo).actions?.Invoke(info, info2); } /// /// 添加无参事件监听 /// /// /// public void AddEventListener(Enum_EventType eventType, UnityAction action) { AddEventListener(eventEnumDic, eventType, action); } /// ///添加有参事件监听 /// /// /// /// public void AddEventListener(Enum_EventType eventType, UnityAction action) { AddEventListener(eventEnumDic, eventType, action); } /// /// 添加多个参数事件监听 /// /// /// /// /// public void AddEventListener(Enum_EventType eventType, UnityAction action) { AddEventListener(eventEnumDic, eventType, action); } /// /// 移除无参事件监听 /// /// /// public void RemoveEventListener(Enum_EventType eventType, UnityAction action) { RemoveEventListener(eventEnumDic, eventType, action); } /// /// 移除有参事件监听 /// /// /// /// public void RemoveEventListener(Enum_EventType eventType, UnityAction action) { RemoveEventListener(eventEnumDic, eventType, action); } /// /// 移除多个参数事件监听 /// /// /// /// /// public void RemoveEventListener(Enum_EventType eventType, UnityAction action) { RemoveEventListener(eventEnumDic, eventType, action); } /// /// 触发无参事件 /// /// public void EventTrigger(Enum_EventType eventType) { EventTrigger(eventEnumDic, eventType); } /// /// 触发有参事件 /// /// /// /// public void EventTrigger(Enum_EventType eventType, T info) { EventTrigger(eventEnumDic, eventType, info); } /// /// 触发多个参数事件 /// /// /// /// /// /// public void EventTrigger(Enum_EventType eventType, T info, D info2) { EventTrigger(eventEnumDic, eventType, info, info2); } /// /// 清楚所有事件 /// public void ClearAllEvent() { eventEnumDic.Clear(); } }