public bool Unregister(OnGameEventRaised handler) { switch (type) { case HandlerType.EventID: return(!string.IsNullOrEmpty(eventID) && GameEventManager.UnregisterHandler(eventID, handler)); case HandlerType.GameEvent: return(gameEvent != null && gameEvent.UnregisterMethod(handler)); default: return(false); } }
public bool UnregisterMethod(OnGameEventRaised method) { if (method == null || invoker.IsEmpty) { return(false); } invoker -= method; if (verbose) { Debug.Log(string.Format("[{0}]: Method Unregistration Succesfull: [{1}]", name, method.Method.Name)); } return(true); }
//----------------------------------------------------- For Methods ---------------------------------------------------// public bool RegisterMethod(OnGameEventRaised method) { if (method == null) { return(false); } if (invoker == null) { invoker = new GameEventDelegate(); } invoker -= method; invoker += method; if (verbose) { Debug.Log(string.Format("[{0}]: Method Registration Succesfull: [{1}]", name, method.Method.Name)); } return(true); }
/// <summary> /// Unregister handler from given eventID /// </summary> /// <param name="eventID"></param> /// <param name="handler"></param> /// <returns>true if successful, false if if either eventID or handler are NULL, or if handler is not currently registered</returns> public static bool UnregisterHandler(string eventID, OnGameEventRaised handler) { if (instance == null) { return(false); } if (!Instance.enabled) { if (Instance.verbose) { Debug.LogWarning( $"Attempting to unregister handler for event [{eventID}] while GameEventManager is disabled!", Instance.gameObject); } return(false); } if (string.IsNullOrEmpty(eventID)) { if (instance.verbose) { Debug.LogWarning("[ERROR] Given eventID to be unregistered is null!", instance.gameObject); } return(false); } if (handler == null) { if (instance.verbose) { Debug.LogWarning("[ERROR] Given handler to be unregistered is null!", instance.gameObject); } return(false); } GameEvent _gameEvent; if (gameEvents.TryGetValue(eventID, out _gameEvent) && _gameEvent != null) { _gameEvent.UnregisterMethod(handler); if (Instance.verbose) { Debug.Log( $"Handler[{handler.Method.Name}] successfully unregistered from GameEvent[{_gameEvent.name}]!", Instance.gameObject); } return(true); } GameEventDelegate _event; if (eventDelegates.TryGetValue(eventID, out _event) && _event != null) { _event -= handler; if (Instance.verbose) { Debug.Log($"Handler[{handler.Method.Name}] successfully unregistered from GameEventDelegate!", Instance.gameObject); } if (!_event.IsEmpty) { return(true); } eventDelegates.Remove(eventID); if (Instance.verbose) { Debug.Log( $"GameEventDelegate for EventID[{eventID}] has no more listeners! Returning instance to pool.", Instance.gameObject); } eventPool.Return(_event); return(true); } return(false); }
//=====================================================================================================================// //=================================================== Public Methods ==================================================// //=====================================================================================================================// #region Public Methods /// <summary> /// Register handler to eventID /// </summary> /// <param name="eventID">EventID to listen to</param> /// <param name="handler">Method delegate</param> /// <returns>True if successfuly registered. False otherwise</returns> public static bool RegisterHandler(string eventID, OnGameEventRaised handler) { if (instance == null) { return(false); } if (!Instance.enabled) { if (Instance.verbose) { Debug.LogWarning($"Attempting to register handler for event [{eventID}] while GameEventManager is disabled!", Instance.gameObject); } return(false); } if (string.IsNullOrEmpty(eventID)) { if (instance.verbose) { Debug.LogWarning("[ERROR] Given eventID to be registered is NULL!", instance.gameObject); } return(false); } if (handler == null) { if (instance.verbose) { Debug.LogWarning("[ERROR] Given handler to be registered is NULL!", instance.gameObject); } return(false); } //Check if GameEvent asset exists and register handler if it does if (gameEvents.TryGetValue(eventID, out var gameEvent) && gameEvent != null) { if (Instance.verbose) { Debug.Log($"Handler[{handler.Method.Name}] successfully registered to GameEvent[{gameEvent.name}]!", Instance.gameObject); } return(gameEvent.RegisterMethod(handler)); } //Check if GameEventDelegate exists and fetch a new one if it doesn't GameEventDelegate evt; if (!eventDelegates.TryGetValue(eventID, out evt) || evt == null) { if (Instance.verbose) { Debug.Log($"EventID [{eventID}] has no listeners! Fetching new GameEventDelegate instance from pool.", Instance.gameObject); } if (eventPool != null) { evt = eventPool.Fetch(); } if (evt == null) { if (Instance.verbose) { Debug.LogError("Something went wrong fetching new GameEventDelegate instance from pool.", Instance.gameObject); } return(false); } eventDelegates.Add(eventID, evt); } evt.name = eventID; evt += handler; if (Instance.verbose) { Debug.Log($"Handler[{handler.Method.Name}] successfully registered to new GameEventDelegate[{eventID}]!", Instance.gameObject); } return(true); }
public void Clear() { name = "INACTIVE"; invoker = null; invoker = (data) => { }; }
public static void RaiseGameEvent(GAME_EVENT type, System.Object data = null) { OnGameEventRaised?.Invoke(type, data); }