public void Update() { float curTime = Time.realtimeSinceStartup; // 在Timeer的Update函数中,调用Timer注册的回调函数,如果在函数中创建新的Timer,则迭代器会报错 var keyList = Pool <uint> .List.New(); foreach (var key in mTimers.Keys) { keyList.Add(key); } //var keyList = new List<uint>(mTimers.Keys); for (int i = 0; i < keyList.Count; ++i) { uint timeId = keyList[i]; DecimalTimer kTime = mTimers[timeId]; if (kTime.Update(curTime - mLastFrameTime)) { mTimeupIds.Add(timeId); } if (kTime.IsDead) { mRemoveIds.Add(timeId); } } Pool <uint> .List.Free(keyList); // 时间已结束的Timer if (mTimeupIds.Count > 0) { for (int i = 0; i < mTimeupIds.Count; ++i) { uint id = mTimeupIds[i]; if (mTimers.ContainsKey(id) == true) { // 时间结束的回调 if (mTimers[id].CallBackFunc != null) { mTimers[id].CallBackFunc(0); } if (mTimers[id].CallBackFuncEx != null) { mTimers[id].CallBackFuncEx(0, mTimers[id]); } } else { Debug.LogError("Error!!!Can not find timer by id: " + id); } } mTimeupIds.Clear(); } // 移除被销毁的Timer if (mRemoveIds.Count > 0) { for (int i = 0; i < mRemoveIds.Count; ++i) { uint id = mRemoveIds[i]; mTimers.Remove(id); } mRemoveIds.Clear(); } mLastFrameTime = curTime; }
public void RegisterTimer(DecimalTimer timer) { mTimers.Add(timer.ID, timer); }