using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using DevelopEngine; 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; } } /// ///1.Dictionary /// 多波委托 使用时注意事件生命周期 及时注销 /// public class EventCenter : MonoSingleton { private Dictionary eventDic = new Dictionary(); /// /// 添加事件监听 /// /// 事件名字 /// 事件 的委托函数 public void AddEventListener(string name, UnityAction action) { if (eventDic.ContainsKey(name)) { (eventDic[name] as EventInfo).actions += action;//将委托事件添加进去 } else { eventDic.Add(name, new EventInfo(action)); } } /// /// 添加事件监听 /// /// 事件名字 /// 事件 的委托函数 public void AddEventListener(string name, UnityAction action) { if (eventDic.ContainsKey(name)) { (eventDic[name] as EventInfo).actions += action;//将委托事件添加进去 } else { eventDic.Add(name, new EventInfo(action)); } } public void AddEventListener(string name, UnityAction action) { if (eventDic.ContainsKey(name)) { (eventDic[name] as EventInfo).actions += action;//将委托事件添加进去 } else { eventDic.Add(name, new EventInfo(action)); } } /// /// 移出事件监听 /// /// /// public void RemoveEnvetListener(string name, UnityAction action) { if (eventDic.ContainsKey(name)) { (eventDic[name] as EventInfo).actions -= action; } } /// /// 移出事件监听 /// /// /// public void RemoveEnvetListener(string name, UnityAction action) { if (eventDic.ContainsKey(name)) { (eventDic[name] as EventInfo).actions -= action; } } public void RemoveEnvetListener(string name, UnityAction action) { if (eventDic.ContainsKey(name)) { (eventDic[name] as EventInfo).actions -= action; } } /// /// 执行事件触发 /// /// 事件名字 public void EventTrigger(string name, T info) { if (eventDic.ContainsKey(name)) { //eventDic[name](); //eventDic[name].Invoke(); if ((eventDic[name] as EventInfo).actions != null) (eventDic[name] as EventInfo).actions.Invoke(info); } } /// /// 执行事件触发 /// /// 事件名字 public void EventTrigger(string name, T info,K info1) { if (eventDic.ContainsKey(name)) { //eventDic[name](); //eventDic[name].Invoke(); if ((eventDic[name] as EventInfo).actions != null) (eventDic[name] as EventInfo).actions.Invoke(info,info1); } } public void EventTrigger(string name) { if (eventDic.ContainsKey(name)) { //eventDic[name](); //eventDic[name].Invoke(); if ((eventDic[name] as EventInfo).actions != null) (eventDic[name] as EventInfo).actions.Invoke(); } } /// /// 清空事件监听 /// public void Clear() { eventDic.Clear(); } }