summaryrefslogtreecommitdiff
path: root/SurvivalTest/Assets/Scripts/Utils/EventCenter.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2022-04-25 16:57:40 +0800
committerchai <chaifix@163.com>2022-04-25 16:57:40 +0800
commitcce2815f3bcfeec2f4c13386d073d20778724656 (patch)
treeb797ed570ffa423587f6032727263fd8b666d4ad /SurvivalTest/Assets/Scripts/Utils/EventCenter.cs
parent48cce250a683024009bdf35a10c6710c615022d6 (diff)
*EventCenter
Diffstat (limited to 'SurvivalTest/Assets/Scripts/Utils/EventCenter.cs')
-rw-r--r--SurvivalTest/Assets/Scripts/Utils/EventCenter.cs48
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