UnityTimer icon indicating copy to clipboard operation
UnityTimer copied to clipboard

Feature request: Restart

Open glenneroo opened this issue 3 years ago • 1 comments

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;
}

glenneroo avatar Aug 15 '21 18:08 glenneroo

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;
    }

eckhardEhm avatar Nov 22 '21 21:11 eckhardEhm