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