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 | |
parent | 48cce250a683024009bdf35a10c6710c615022d6 (diff) |
*EventCenter
Diffstat (limited to 'SurvivalTest/Assets/Scripts/Utils')
-rw-r--r-- | SurvivalTest/Assets/Scripts/Utils/EventCenter.cs | 48 | ||||
-rw-r--r-- | SurvivalTest/Assets/Scripts/Utils/EventCenter.cs.meta | 11 | ||||
-rw-r--r-- | SurvivalTest/Assets/Scripts/Utils/EventType.cs | 12 | ||||
-rw-r--r-- | SurvivalTest/Assets/Scripts/Utils/EventType.cs.meta | 11 |
4 files changed, 82 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 diff --git a/SurvivalTest/Assets/Scripts/Utils/EventCenter.cs.meta b/SurvivalTest/Assets/Scripts/Utils/EventCenter.cs.meta new file mode 100644 index 0000000..f87c419 --- /dev/null +++ b/SurvivalTest/Assets/Scripts/Utils/EventCenter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 224d79fa30383194d86f18f391400340 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/SurvivalTest/Assets/Scripts/Utils/EventType.cs b/SurvivalTest/Assets/Scripts/Utils/EventType.cs new file mode 100644 index 0000000..92bc690 --- /dev/null +++ b/SurvivalTest/Assets/Scripts/Utils/EventType.cs @@ -0,0 +1,12 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public enum EventMsgType +{ + + // UI + ToggleAutoFire, // 自动开火 + ToggleLockAim, // 锁定方向 +} + diff --git a/SurvivalTest/Assets/Scripts/Utils/EventType.cs.meta b/SurvivalTest/Assets/Scripts/Utils/EventType.cs.meta new file mode 100644 index 0000000..90ea046 --- /dev/null +++ b/SurvivalTest/Assets/Scripts/Utils/EventType.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2cca452167d1ce34cb98da00cae563b0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |