public static void TriggerEvent(IEMEvent e) { EventDelegate del; if (delegates.TryGetValue(e.GetType(), out del)) { del.Invoke(e); // remove listeners which should only be called once foreach (EventDelegate k in delegates[e.GetType()].GetInvocationList()) { if (onceLookups.ContainsKey(k)) { delegates[e.GetType()] -= k; if (delegates[e.GetType()] == null) { delegates.Remove(e.GetType()); } delegateLookup.Remove(onceLookups[k]); onceLookups.Remove(k); } } } else { //Debug.LogWarning ("Event: " + e.GetType () + " has no listeners"); } }
//Inserts the event into the current queue. public static bool QueueEvent(IEMEvent evt) { if (!delegates.ContainsKey(evt.GetType())) { //Debug.LogWarning ("EventManager: QueueEvent failed due to no listeners for event: " + evt.GetType ()); return(false); } m_eventQueue.Enqueue(evt); return(true); }
//Every update cycle the queue is processed, if the queue processing is limited, //a maximum processing time per update can be set after which the events will have //to be processed next update loop. void Update() { float timer = 0.0f; while (m_eventQueue.Count > 0) { if (limitQueueProcesing) { if (timer > queueProcessTime) { return; } } evt = m_eventQueue.Dequeue() as IEMEvent; TriggerEvent(evt); if (limitQueueProcesing) { timer += Time.deltaTime; } } }