UnityTimer
UnityTimer copied to clipboard
Feature request: Restart
It would be useful in some special cases to be able to restart a running timer from the beginning. Maybe this functionality already exists? Here is my attempt, but maybe I missed something:
/// <summary>
/// Restarts a running or paused timer.
/// </summary>
public void Restart()
{
if (this.isDone)
{
return;
}
this._timeElapsedBeforePause = null;
this._startTime = this.GetWorldTime();
this.isCompleted = false;
}
Afaik after completion the timer cannot be reused. Changing .isCompleted will not give you the result you seek as the timer already has been removed from the TimerManager List of Timers.
So if the timer is completed, I start a new one. If it is still running you can reset the time.
/// <summary>
/// Restart a Timer by Setting the Starttime and LastUpdatetime to Timer.GetWorldTime(). Returns true if it had not Completed, false if the Timer is Completed.
/// </summary>
public bool Restart()
{
if (!this.isCompleted)
{
this._startTime = this.GetWorldTime();
this._lastUpdateTime = this._startTime;
return true;
}
return false;
}