using System; using System.Collections; using System.Collections.Generic; using UnityEditor.Search; using UnityEngine; //消息管理器 public class EventManager : MonoSingleton { public delegate void EventHandler(params object[] args); private Dictionary _actionDic = new Dictionary(); private Dictionary _actionsDic = new Dictionary(); public void AddEventListener(string eventName, Action handler) { if (!_actionDic.ContainsKey(eventName)) { _actionDic.Add(eventName, handler); } else { _actionDic[eventName] -= handler; _actionDic[eventName] += handler; } } public void RemoveEventListener(string eventName, Action handler) { if (!_actionDic.ContainsKey(eventName)) return; _actionDic[eventName] -= handler; } public void Dispatch(string eventName) { if (!_actionDic.ContainsKey(eventName)) return; _actionDic[eventName]?.Invoke(); } //带参数的事件 public void AddEventListener(string eventName, EventHandler handler) { if (!_actionsDic.ContainsKey(eventName)) { _actionsDic.Add(eventName, handler); } else { _actionsDic[eventName] -= handler; _actionsDic[eventName] += handler; } } public void RemoveEventListener(string eventName, EventHandler handler) { if (!_actionsDic.ContainsKey(eventName)) return; _actionsDic[eventName] -= handler; } public void Dispatch(string eventName, params object[] param) { if (!_actionsDic.ContainsKey(eventName)) return; _actionsDic[eventName]?.Invoke(param); } }