using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; /// /// 用于 里式替换原则 装载 子类的父类 /// public abstract class EventInfoBase{ } /// /// 用来包裹 对应观察者 函数委托的 类 /// /// public class EventInfo:EventInfoBase { //真正观察者 对应的 函数信息 记录在其中 public UnityAction actions; public EventInfo(UnityAction action) { actions += action; } } /// /// 主要用来记录无参无返回值委托 /// public class EventInfo: EventInfoBase { public UnityAction actions; public EventInfo(UnityAction action) { actions += action; } } /// /// 事件中心模块 /// public class EventCenter: BaseManager { //用于记录对应事件 关联的 对应的逻辑 private Dictionary eventDic = new Dictionary(); private EventCenter() { } /// /// 触发事件 /// /// 事件名字 public void EventTrigger(E_EventType eventName, T info) { //存在关心我的人 才通知别人去处理逻辑 if(eventDic.ContainsKey(eventName)) { //去执行对应的逻辑 (eventDic[eventName] as EventInfo).actions?.Invoke(info); } } /// /// 触发事件 无参数 /// /// public void EventTrigger(E_EventType eventName) { //存在关心我的人 才通知别人去处理逻辑 if (eventDic.ContainsKey(eventName)) { //去执行对应的逻辑 (eventDic[eventName] as EventInfo).actions?.Invoke(); } } /// /// 添加事件监听者 /// /// /// public void AddEventListener(E_EventType eventName, UnityAction func) { //如果已经存在关心事件的委托记录 直接添加即可 if (eventDic.ContainsKey(eventName)) { (eventDic[eventName] as EventInfo).actions += func; } else { eventDic.Add(eventName, new EventInfo(func)); } } public void AddEventListener(E_EventType eventName, UnityAction func) { //如果已经存在关心事件的委托记录 直接添加即可 if (eventDic.ContainsKey(eventName)) { (eventDic[eventName] as EventInfo).actions += func; } else { eventDic.Add(eventName, new EventInfo(func)); } } /// /// 移除事件监听者 /// /// /// public void RemoveEventListener(E_EventType eventName, UnityAction func) { if (eventDic.ContainsKey(eventName)) (eventDic[eventName] as EventInfo).actions -= func; } public void RemoveEventListener(E_EventType eventName, UnityAction func) { if (eventDic.ContainsKey(eventName)) (eventDic[eventName] as EventInfo).actions -= func; } /// /// 清空所有事件的监听 /// public void Clear() { eventDic.Clear(); } /// /// 清除指定某一个事件的所有监听 /// /// public void Claer(E_EventType eventName) { if (eventDic.ContainsKey(eventName)) eventDic.Remove(eventName); } }