using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.Windows; /// /// 事件中心 /// 1.字典 2.委托 3.观察者设计模式 /// public class EventCenter : BaseManager { //key ---事件名字(比如:怪物死亡,玩家死亡,通关 等等) //value --- 对应 监听这个事件 对应的委托函数 private Dictionary> eventDic = new Dictionary>(); /// /// 添加事件监听 /// /// 事件的名字 /// 准备用来处理时间的委托函数 public void AddEventListener(string name,UnityAction action) { //有没有对应的事件监听 //有的情况 if(eventDic.ContainsKey(name)) { eventDic[name] += action;//已经有事件就往后排 } //没有的情况 else { eventDic.Add(name, action);//没有就加一个 } } /// /// 减去事件监听 /// 事件有加就有减 /// /// /// public void RemoveEventListener(string name, UnityAction action) { if(eventDic.ContainsKey(name)) { eventDic[name] -= action; } } /// /// 事件触发 /// /// 哪一个名字的事件触发了 public void EvennTrigger(string name,object info) { if(eventDic.ContainsKey(name)) { eventDic[name].Invoke(info);//有就触发事件 } } /// /// 清空事件中心,主要用在场景切换时 /// public void Clear() { eventDic.Clear(); } }