public bool DetachListener(int eventKey, DGameEventHandle eventHandle) { if (!mListenerTable.ContainsKey(eventKey)) { return(false); } List <CEventData> listenerList = mListenerTable[eventKey]; CEventData find = null; foreach (CEventData evtData in listenerList) { if (evtData.mHandle == eventHandle) { find = evtData; break; } } if (find != null) { listenerList.Remove(find); } return(true); }
/// <summary> /// 同步事件触发 /// </summary> /// <param name="evt"></param> /// <returns></returns> public void TriggerEvent(IEvent evt) { int eventKey = evt.GetKey(); List <CEventData> listenerList = null; if (mListenerTable.TryGetValue(eventKey, out listenerList)) { //防止在事件处理流程中改变事件列表 List <CEventData> tmpList = new List <CEventData>(listenerList); for (int i = 0; i < tmpList.Count; ++i) { CEventData evtData = tmpList[i]; if (evtData.mHandle != null && (!evtData.mObjRelate || evtData.mGameObj != null)) { #if PROFILE //Profiler.BeginSample(string.Format("HandleEvt: {0}, {1}", eventKey, evtData.mGameObj != null ? evtData.mGameObj.name : "null")); #endif evtData.mHandle(evt); #if PROFILE //Profiler.EndSample(); #endif } } for (int i = 0; i < tmpList.Count; ++i) { if (tmpList[i].mObjRelate && tmpList[i].mGameObj == null) { listenerList.Remove(tmpList[i]); } } } }
/// <summary> /// 同步事件触发 /// </summary> /// <param name="evt"></param> /// <returns></returns> public void TriggerEvent(IEvent evt) { int eventKey = evt.GetKey(); List <CEventData> listenerList = null; if (mListenerTable.TryGetValue(eventKey, out listenerList)) { //防止在事件处理流程中改变事件列表 List <CEventData> tmpList = new List <CEventData>(listenerList); for (int i = 0; i < tmpList.Count; ++i) { CEventData evtData = tmpList[i]; if (evtData.mHandle != null && (!evtData.mObjRelate || evtData.mGameObj != null)) { evtData.mHandle(evt); } } for (int i = 0; i < tmpList.Count; ++i) { if (tmpList[i].mObjRelate && tmpList[i].mGameObj == null) { listenerList.Remove(tmpList[i]); } } } //改变事件等待协程的状态 if (mEvtWaiterMap.ContainsKey(eventKey)) { foreach (CEvtWaiter waiter in mEvtWaiterMap[eventKey]) { waiter.mEvt = evt; } mEvtWaiterMap[eventKey].Clear(); mEvtWaiterMap[eventKey] = null; mEvtWaiterMap.Remove(eventKey); } }
public bool DetachListener(int eventKey, DGameEventHandle eventHandle) { if (!mListenerTable.ContainsKey(eventKey)) { return(false); } List <CEventData> listenerList = mListenerTable[eventKey]; CEventData find = null; for (int i = 0; i < listenerList.Count; ++i) { if (listenerList[i].mHandle == eventHandle) { find = listenerList[i]; break; } } if (find != null) { listenerList.Remove(find); } return(true); }
/// <summary> /// 同步事件触发 /// </summary> /// <param name="evt"></param> /// <returns></returns> public bool TriggerEvent(IEvent evt) { #if PROFILE Profiler.BeginSample("TriggerEvent"); #endif bool ret = true; ESysEvent eventKey = (ESysEvent)evt.GetKey(); if (!mListenerTable.ContainsKey(eventKey)) { ret = false; } else { List <CEventData> listenerList = mListenerTable[eventKey]; //防止在事件处理流程中改变事件列表 List <CEventData> tmpList = new List <CEventData>(listenerList); for (int i = 0; i < tmpList.Count; ++i) //foreach (CEventData evtData in tmpList) { CEventData evtData = tmpList[i]; if (evtData.mHandle != null && (!evtData.mObjRelate || evtData.mGameObj != null)) { #if PROFILE Profiler.BeginSample(string.Format("HandleEvt: {0}, {1}", eventKey, evtData.mGameObj != null ? evtData.mGameObj.name : "null")); #endif evtData.mHandle(evt); #if PROFILE Profiler.EndSample(); #endif } } for (int i = 0; i < tmpList.Count; ++i) { if (tmpList[i].mObjRelate && tmpList[i].mGameObj == null) { listenerList.Remove(tmpList[i]); } } } //改变事件等待协程的状态 if (mEvtWaiterMap.ContainsKey(eventKey)) { foreach (CEvtWaiter waiter in mEvtWaiterMap[eventKey]) { waiter.mEvt = evt; } mEvtWaiterMap[eventKey].Clear(); mEvtWaiterMap[eventKey] = null; mEvtWaiterMap.Remove(eventKey); } #if PROFILE Profiler.EndSample(); #endif return(ret); }