public void SetTimer(ref TimerHandler handler, TimerCallback callback, float rate, object target, Func <object, bool> validateFunction = null, int loopCount = 1, float inFirstDelay = -1.0f, bool pause = false) { StopTimer(ref handler); DebugUtility.AssertFormat(callback != null, "Please ensure that the callback is not null."); DebugUtility.AssertFormat(loopCount != 0, "Please ensure that the loopCount is not Zero."); if ((target != null) && (validateFunction == null)) { // Set validate function at SetTimer is better that DoValidate to check some types. validateFunction = GetValidateFunction(target); } DebugUtility.AssertFormat((target == null && validateFunction == null) || (target != null && validateFunction != null), "Please "); float duration; if (inFirstDelay > 0.0f) { duration = inFirstDelay; } else { duration = rate; } handler = new TimerHandler(UniqueID.New()); Timer timer; timer.rate = rate; timer.loopCount = loopCount; timer.callback = callback; timer.handler = handler; timer.duration = duration; timer.pause = pause; bool requiringTarget = timer.requiringTarget = (target != null) && ((validateFunction != null) && validateFunction(target)); timer.target = (requiringTarget ? target : null); // for avoid the invalid reference (The Unity design reason) timer.validateFunction = validateFunction; mRunningTimers.Add(timer); }
public void PauseTimer(TimerHandler handler) { if (!handler.IsValid) { return; } ProfilingUtility.BeginSample("TimerManager.PauseTimer"); int timerCount = mRunningTimers.Count; for (int idx = 0; idx < timerCount; ++idx) { Timer timer = mRunningTimers[idx]; if (timer.handler == handler) { timer.pause = true; mRunningTimers[idx] = timer; break; } } ProfilingUtility.EndSample(); }
public void StopTimer(ref TimerHandler handler) { if (!handler.IsValid) { return; } ProfilingUtility.BeginSample("TimerManager.StopTimer"); int timerCount = mRunningTimers.Count; for (int idx = 0; idx < timerCount; ++idx) { Timer timer = mRunningTimers[idx]; if (timer.handler == handler) { handler.SetExpired(); timer.pause = true; timer.loopCount = 0; mRunningTimers[idx] = timer; break; } } ProfilingUtility.EndSample(); }