diff options
author | chai <chaifix@163.com> | 2022-04-25 16:57:40 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2022-04-25 16:57:40 +0800 |
commit | cce2815f3bcfeec2f4c13386d073d20778724656 (patch) | |
tree | b797ed570ffa423587f6032727263fd8b666d4ad /SurvivalTest/Assets/Scripts/Utils/EventCenter.cs | |
parent | 48cce250a683024009bdf35a10c6710c615022d6 (diff) |
*EventCenter
Diffstat (limited to 'SurvivalTest/Assets/Scripts/Utils/EventCenter.cs')
-rw-r--r-- | SurvivalTest/Assets/Scripts/Utils/EventCenter.cs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/SurvivalTest/Assets/Scripts/Utils/EventCenter.cs b/SurvivalTest/Assets/Scripts/Utils/EventCenter.cs new file mode 100644 index 0000000..eef3646 --- /dev/null +++ b/SurvivalTest/Assets/Scripts/Utils/EventCenter.cs @@ -0,0 +1,48 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class EventCenter : Singleton<EventCenter> +{ + public delegate void EventHandler(params object [] param); + + private Dictionary<EventMsgType, List<EventHandler>> m_EventHandlers = new Dictionary<EventMsgType, List<EventHandler>>(); + + public void Register(EventMsgType evt, EventHandler handler) + { + List<EventHandler> handlers; + if (!m_EventHandlers.TryGetValue(evt, out handlers)) + { + handlers = new List<EventHandler>(); + m_EventHandlers.Add(evt, handlers); + } + if (!handlers.Contains(handler)) + { + handlers.Add(handler); + } + else + { + Debug.LogError("Event has already registed, eventType=" + evt); + } + } + + public void UnRegister(EventMsgType evt, EventHandler handler) + { + List<EventHandler> handlers; + if (m_EventHandlers.TryGetValue(evt, out handlers)) + { + if(handlers.Contains(handler)) + handlers.Remove(handler); + } + } + + public void Publish(EventMsgType evt, params object[] objs) + { + List<EventHandler> handlers; + if (m_EventHandlers.TryGetValue(evt, out handlers)) + { + handlers.ForEach(h => h(objs)); + } + } + +}
\ No newline at end of file |