Last active
September 29, 2020 07:12
-
-
Save WooshiiDev/78a9326ec5936608f0f74968d389664f to your computer and use it in GitHub Desktop.
Unity Timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Simple manual timer class | |
/// </summary> | |
[System.Serializable] | |
public class Timer | |
{ | |
public float startTime = 0; | |
public float endTime = 0; | |
public bool IsFinished => currentTime >= endTime; | |
[UnityEngine.SerializeField] | |
private float currentTime; | |
public Timer(float endTime) | |
{ | |
this.endTime = endTime; | |
} | |
/// <summary> | |
/// Update the timer value | |
/// </summary> | |
/// <param name="deltaTime"></param> | |
public void UpdateTimer(float deltaTime) | |
{ | |
if (currentTime > endTime) | |
currentTime = startTime; | |
else | |
currentTime += deltaTime; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/// <summary> | |
/// Manually updated timer class | |
/// </summary> | |
[System.Serializable] | |
public class Timer | |
{ | |
public float startTime = 0; | |
public float endTime = 0; | |
[UnityEngine.SerializeField] | |
private float currentTime; | |
public Action onFinish; | |
public float CurrentTime => currentTime; | |
public bool IsFinished => currentTime >= endTime; | |
public bool IsStarting => currentTime <= startTime; | |
public float CurrentTimeNormalized => currentTime / endTime; | |
public Timer(float endTime = 1, float startTime = 0, float currentTime = 0) | |
{ | |
this.startTime = startTime; | |
this.endTime = endTime; | |
this.currentTime = currentTime; | |
} | |
/// <summary> | |
/// Update the timer value | |
/// </summary> | |
/// <param name="deltaTime"></param> | |
public void UpdateTimer(float deltaTime) | |
{ | |
if (currentTime >= endTime) | |
currentTime = startTime; | |
else | |
currentTime += deltaTime; | |
if (IsFinished) | |
onFinish?.Invoke (); | |
} | |
/// <summary> | |
/// Manually reset the timer to it's start time | |
/// </summary> | |
public void ResetTimer() | |
{ | |
currentTime = 0; | |
} | |
/// <summary> | |
/// Set the timer position within range of min and max (inclusive) | |
/// </summary> | |
/// <param name="value">New value for the timer</param> | |
public void SetTimerPosition(float value) | |
{ | |
currentTime = UnityEngine.Mathf.Clamp (value, startTime, endTime); | |
} | |
public void SetTimerPositionNormalized(float value) | |
{ | |
currentTime = endTime * value; | |
} | |
public override String ToString() | |
{ | |
return $"Timer: {currentTime}; {startTime} | {endTime}"; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment