コード例 #1
0
        /// <summary>
        /// Register to listen for eventID.
        /// </summary>
        /// <param name="eventID">EventID that object want to listen</param>
        /// <param name="callback">Callback will be invoked when this eventID be raised</param>
        public void RegisterListener(ObserverEventID eventID, Action <object> callback)
        {
            // checking params
            Debug.Assert(callback != null, string.Format("AddListener, event {0}, callback is null !!", eventID.ToString()));
            Debug.Assert(eventID != ObserverEventID.None, "RegisterListener, event = None !!");

            // check if listener exist in distionary
            if (listenersDict.ContainsKey(eventID))
            {
                // add callback to our collection
                listenersDict[eventID] += callback;
            }
            else
            {
                // add new key-value pair
                listenersDict.Add(eventID, null);
                listenersDict[eventID] += callback;
            }
        }
コード例 #2
0
        /// <summary>
        /// Removes the listener. Use to unregister listener.
        /// </summary>
        /// <param name="eventID">Event that object want to listen.</param>
        /// <param name="callback">Callback action.</param>
        public void RemoveListener(ObserverEventID eventID, Action <object> callback)
        {
            // checking params
            Debug.Assert(callback != null, string.Format("RemoveListener, event {0}, callback is null.", eventID.ToString()));
            Debug.Assert(eventID != ObserverEventID.None, "AddListener, event = None.");

            if (listenersDict.ContainsKey(eventID))
            {
                listenersDict[eventID] -= callback;
            }
            else
            {
                Debug.LogWarning("RemoveListener, tried to remove nonexistent key : " + eventID);
            }
        }