From cce2815f3bcfeec2f4c13386d073d20778724656 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Apr 2022 16:57:40 +0800 Subject: *EventCenter --- SurvivalTest/Assets/Scripts/Utils/EventCenter.cs | 48 ++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 SurvivalTest/Assets/Scripts/Utils/EventCenter.cs (limited to 'SurvivalTest/Assets/Scripts/Utils/EventCenter.cs') 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 +{ + public delegate void EventHandler(params object [] param); + + private Dictionary> m_EventHandlers = new Dictionary>(); + + public void Register(EventMsgType evt, EventHandler handler) + { + List handlers; + if (!m_EventHandlers.TryGetValue(evt, out handlers)) + { + handlers = new List(); + 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 handlers; + if (m_EventHandlers.TryGetValue(evt, out handlers)) + { + if(handlers.Contains(handler)) + handlers.Remove(handler); + } + } + + public void Publish(EventMsgType evt, params object[] objs) + { + List handlers; + if (m_EventHandlers.TryGetValue(evt, out handlers)) + { + handlers.ForEach(h => h(objs)); + } + } + +} \ No newline at end of file -- cgit v1.1-26-g67d0