/// <summary> /// Sets a timer to call a given method at a set interval. /// Frequency rates of 0.0 will disable the previous timer. /// </summary> /// <param name="inFrequency">The amount of time to pass before an execution occurs.</param> /// <param name="inLoop">Whether to execute once or execute repeatedly.</param> /// <param name="inMethodName">Name of the method to call when the timer executes.</param> public void SetTimer(float inFrequency, bool inLoop, string inMethodName, bool inIgnoreTimeScale = false) { // Validate the method name before continuing. if (string.IsNullOrEmpty(inMethodName)) { Debug.Log("Failed to create timer. String is empty"); return; } bool isDupe = false; // Search for a duplicate before creating a new instance. for (int timerIdx = 0; timerIdx < _timers.Count; timerIdx++) { TimerBehaviourData timer = _timers[timerIdx]; if (timer.methodName == inMethodName) { isDupe = true; // If the frequency is 0, flag it for removal if (inFrequency == 0.0f) { // We'll remove this timer next time we tick all the timers. timer.frequency = 0.0f; } else { // Update the timer with the new info. timer.loop = inLoop; timer.frequency = inFrequency; timer.elpasedTime = 0.0f; } timer.paused = false; break; } } // If this timer is not a dupe, create a new one. if (!isDupe) { TimerBehaviourData newTimer = new TimerBehaviourData( ); newTimer.loop = inLoop; newTimer.action = null; newTimer.paused = false; newTimer.frequency = inFrequency; newTimer.methodName = inMethodName; newTimer.elpasedTime = 0.0f; _timers.Add(newTimer); } }
/// <summary> /// Sets a timer to call the given action at a set interval. /// Frequency rates of 0.0 will disable the previous timer. /// /// Note: /// Anonymous methods cannot be searched or updated. Use anonymous /// methods for single shot script calls. /// </summary> /// <param name="inFrequency">The amount of time to pass between executions.</param> /// <param name="inLoop">Whether to execute once or execute repeatedly.</param> /// <param name="inAction">The action to invoke when this timer executes.</param> public void SetTimer(float inFrequency, bool inLoop, Action inAction) { bool isDupe = false; // Search for a duplicate before creating a new instance. for (int timerIdx = 0; timerIdx < _timers.Count; timerIdx++) { TimerBehaviourData timer = _timers[timerIdx]; if (timer.action == inAction) { isDupe = true; // If the frequency is 0, disable the timer. if (inFrequency == 0.0f) { // We'll remove this timer next time we tick all the timers. timer.frequency = 0.0f; } else { // Update the timer with the new info. timer.loop = inLoop; timer.frequency = inFrequency; timer.elpasedTime = 0.0f; } timer.paused = false; break; } } // If this timer is not a dupe, create a new one. if (!isDupe) { TimerBehaviourData newTimer = new TimerBehaviourData( ); newTimer.loop = inLoop; newTimer.action = inAction; newTimer.paused = false; newTimer.frequency = inFrequency; newTimer.methodName = ""; newTimer.elpasedTime = 0.0f; _timers.Add(newTimer); } }