private void ProcessTimers() { float dt = UnityEngine.Time.deltaTime; for (int i = globalTimers.Count - 1; i >= 0; i--) { TimerElement timer = globalTimers[i]; timer.timeLeft -= dt; if (timer.timeLeft <= 0) { timer.timerCallback(); // if repeatTime is atm 1 and is about to change to 0, remove it. // if repeatTimes is 0 => infinite looping if (timer.repeatTimes == 1) { globalTimers.RemoveAt(i); continue; } else if (timer.repeatTimes > 1) { timer.repeatTimes--; } timer.timeLeft = timer.interval; } } }
/// <summary> /// Merges data into your object. (no deep copy) /// </summary> /// <param name="incoming"></param> /// <param name="onlyCopyPersistedData"></param> public void MergeDataFrom(TimerElement incoming, bool onlyCopyPersistedData = false) { if (incoming == null) { UnityEngine.Debug.LogError("Trying to merge from null! Type: TimerElement"); return; } // base.MergeDataFrom(incoming, onlyCopyPersistedData); if (!onlyCopyPersistedData) { this.info = incoming.info; } if (!onlyCopyPersistedData) { this.timeLeft = incoming.timeLeft; } if (!onlyCopyPersistedData) { this.interval = incoming.interval; } if (!onlyCopyPersistedData) { this.repeatTimes = incoming.repeatTimes; } if (!onlyCopyPersistedData) { this.timerCallback = incoming.timerCallback; } }
/// <summary> /// Adds a timer in the global update-method and calls the callback n-times (or infinite till application end) /// <param name="interval"></param> /// <param name="callback"></param> /// <param name="repeatTimes"></param> /// <param name="info"></param> /// </summary> public override TimerElement CreateGlobalTimer(float interval, Action callback, int repeatTimes, string info = "") { TimerElement timer = new TimerElement() { info = info, timerCallback = callback, repeatTimes = repeatTimes, interval = interval, timeLeft = interval }; globalTimers.Add(timer); return(timer); }
//docs come here public override void RemoveGlobalTimer(TimerElement timer) { globalTimers.Remove(timer); }
public abstract void RemoveGlobalTimer(TimerElement timer);