static void CheckCreate() { if (!Application.isPlaying) { throw new System.InvalidOperationException("CheckCreate can only be called at play mode."); } if (goCommonAppController == null) { mainThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId; var go = new GameObject("_TaskManager"); goCommonAppController = go; appBehaviour_ = go.AddComponent <DummyMonoBehaviour>(); unbreakableAppBehaviour_ = go.AddComponent <DummyMonoBehaviour>(); appController_ = go.AddComponent <ApplicationController>(); appEventDelegate_ = new AppEventDelegate(); appController_.appPauseDelegate = appEventDelegate_; appController_.appUpdateDelegate = appEventDelegate_; appController_.appLateUpdateDelegate = appEventDelegate_; appEventDelegate_.onAppUpdate += Update; GameObject.DontDestroyOnLoad(go); } }
/// <summary> /// Triggers the event causing the delegates to be called /// </summary> /// <param name="a_eEvent"> Enumerated event name</param> /// <param name="args">array of arguments that are passed to the delegates</param> public void TriggerEvent <T>(IEventBase a_refEvent) where T : IEventBase { Type t = typeof(T); if (m_bEnableLogs) { Debug.Log("[EventManager] trigger Event " + t.ToString()); } if (m_lstEventRegistry.ContainsKey(t)) { AppEventDelegate d = m_lstEventRegistry[t]; if (d != null) { d(a_refEvent); if (m_bEnableLogs) { Debug.Log("[EventManager] trigger Event " + t.ToString()); } } else { //This an error. Console.WriteLine("Could not trigger event: "); } } }
/// <summary> /// deregisters the event delegate from the dictionary. /// The delegate will not be called when the event is fired after this call /// </summary> /// <param name="a_eEvent"> Enumerated event name </param> /// <param name="a_delListener">Callback that is to be called when the event is triggered</param> public void DeRegisterEvent <T>(AppEventDelegate a_delListener) where T : IEventBase { Type t = typeof(T); if (!m_lstEventRegistry.ContainsKey(t)) { return; } m_lstEventRegistry[t] -= a_delListener; if (m_bEnableLogs) { Debug.Log("[EventManager] DeRegister Event " + t.ToString()); } }
/// <summary> /// Registers a callback delegate to the specified event /// </summary> /// <param name="a_eEvent"> Enumerated event name</param> /// <param name="a_delListener"> Callback that is to be called when the event is triggered </param> public void RegisterEvent <T>(AppEventDelegate a_delListener) where T : IEventBase { Type t = typeof(T); if (!m_lstEventRegistry.ContainsKey(t)) { m_lstEventRegistry.Add(typeof(T), a_delListener); return; } //AppEventDelegate exsisting = m_lstEventRegistry[t]; m_lstEventRegistry[t] += a_delListener; if (m_bEnableLogs) { Debug.Log("[EventManager] Register Event " + t.ToString()); } }