Fixed some timer bugs

Timer now has a mode for unscaled time
FIXED: Countdown now resets properly
This commit is contained in:
Gerard Gascón 2021-06-12 10:36:38 +02:00 committed by GitHub
parent 750d7f6b88
commit a4c26426e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 9 deletions

View file

@ -17,13 +17,21 @@ namespace SimpleTools.Timer{
public TMP_Text TimerText { get { return timer; } }
TimerType timerType;
public TimerType TimerType { get { return timerType; } }
TimerUpdate timerUpdate;
public TimerUpdate TimerUpdate { get { return timerUpdate; } }
public void Setup(float elapsedTime, bool isPaused, TimeSpan timePlaying, TMP_Text timer, TimerType timerType, string text){
this.elapsedTime = elapsedTime;
float defaultTime;
/// <summary>
/// Setup the timer
/// </summary>
public void Setup(float elapsedTime, bool isPaused, TimeSpan timePlaying, TMP_Text timer, TimerType timerType, TimerUpdate timerUpdate, string text){
this.elapsedTime = defaultTime = elapsedTime;
this.isPaused = isPaused;
this.timePlaying = timePlaying;
this.timer = timer;
this.timerType = timerType;
this.timerUpdate = timerUpdate;
timer.text = text;
}
@ -34,14 +42,14 @@ namespace SimpleTools.Timer{
}else{
switch (timerType){
case TimerType.Countdown:
elapsedTime -= Time.deltaTime;
elapsedTime -= timerUpdate == TimerUpdate.UnscaledTime ? Time.unscaledDeltaTime : Time.deltaTime;
if(elapsedTime < 0f){
elapsedTime = 0f;
isPaused = true;
}
break;
case TimerType.Stopwatch:
elapsedTime += Time.deltaTime;
elapsedTime += timerUpdate == TimerUpdate.UnscaledTime ? Time.unscaledDeltaTime : Time.deltaTime;
break;
}
timePlaying = TimeSpan.FromSeconds(elapsedTime);
@ -51,25 +59,37 @@ namespace SimpleTools.Timer{
}
}
/// <summary>
/// Play or resume the timer
/// </summary>
public void Play(){
isPaused = false;
StartCoroutine(UpdateTimer());
}
/// <summary>
/// Pause the timer
/// </summary>
public void Stop(){
isPaused = true;
}
/// <summary>
/// Pause and sets the time to the defaultOne
/// </summary>
public void ResetTimer(){
isPaused = true;
elapsedTime = 0f;
elapsedTime = defaultTime;
timePlaying = TimeSpan.FromSeconds(elapsedTime);
timer.text = timePlaying.ToString("m':'ss'.'ff");
}
/// <summary>
/// Restarts the timer
/// </summary>
public void Restart(){
isPaused = false;
elapsedTime = 0f;
elapsedTime = defaultTime;
timePlaying = TimeSpan.FromSeconds(elapsedTime);
timer.text = timePlaying.ToString("m':'ss'.'ff");
StopAllCoroutines();

View file

@ -0,0 +1,6 @@
namespace SimpleTools.Timer{
public enum TimerUpdate{
ScaledTime,
UnscaledTime,
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 977b7b3a05b17c04da63124856e9ff6f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -6,7 +6,14 @@ using TMPro;
namespace SimpleTools.Timer{
public static class TimerUtility {
public static Timer SetupTimer(this TMP_Text container, TimerType timerType, float countdownTime = 60f){
/// <summary>
/// Setup the timer
/// </summary>
/// <param name="container">TMPro object that will contain the timer</param>
/// <param name="timerType">What type of timer will it be (Countdown, Stopwatch, Clock)</param>
/// <param name="countdownTime">The time that will have in case it is a countdown timer</param>
/// <returns></returns>
public static Timer SetupTimer(this TMP_Text container, TimerType timerType, TimerUpdate timerUpdate, float countdownTime = 60f){
Timer t = container.gameObject.AddComponent<Timer>();
float elapsedTime = 0f;
string text = string.Empty;
@ -21,7 +28,7 @@ namespace SimpleTools.Timer{
text = DateTime.Now.ToString("HH:mm:ss");
break;
}
t.Setup(elapsedTime, true, timePlaying, container, timerType, text);
t.Setup(elapsedTime, true, timePlaying, container, timerType, timerUpdate, text);
return t;
}

View file

@ -1,6 +1,6 @@
{
"name": "com.geri.simpletools",
"version": "1.1.0",
"version": "1.1.1",
"displayName": "Simple Tools",
"description": "This package contains simple tools to use in your project.",
"unity": "2018.4",