using System.Collections; using System.Collections.Generic; using UnityEngine.Events; namespace ProjectBase.Event { public delegate void EventHandler(BaseEventData eventData); public class EventCenter : BaseManager { public const int Infinity = 999999; private Dictionary eventsDic = new Dictionary(); /// /// 添加事件 /// /// /// /// /// public void RegisterEvent(string key, EventHandler handler, int time = Infinity, Dictionary dic = null) { Dictionary refDic = dic == null ? eventsDic : dic; EventHandlerItem eventHandlerItem = null; if (eventsDic.TryGetValue(key, out eventHandlerItem)) { eventHandlerItem.AddEventHandler(handler, time); } else { eventsDic.Add(key, new EventHandlerItem(key, handler, time)); } } /// /// 移除事件 /// /// /// /// public void UnregisterEvent(string key, EventHandler handler, Dictionary dic = null) { Dictionary refDic = dic == null ? eventsDic : dic; EventHandlerItem eventHandlerItem = null; if (eventsDic.TryGetValue(key, out eventHandlerItem)) { eventHandlerItem.RemoveEventHandler(handler); } } /// /// 触发事件 /// /// /// public void RunEvent(string key, BaseEventData eventData, Dictionary dic = null) { Dictionary refDic = dic == null ? eventsDic : dic; EventHandlerItem eventHandlerItem = null; if (eventsDic.TryGetValue(key, out eventHandlerItem)) { eventHandlerItem.EventHandlerTrigger(eventData); } } /// /// 删除 /// /// /// public void DeleteEventByKey(string key, Dictionary dic = null) { Dictionary refDic = dic == null ? eventsDic : dic; refDic.Remove(key); } /// /// 重置 /// /// public void Reset(Dictionary dic = null) { Dictionary refDic = dic == null ? eventsDic : dic; refDic.Clear(); } } }