From a4c26426e5d519e39eea9a3adc53581127bb4b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Gasc=C3=B3n?= <52170489+GerardGascon@users.noreply.github.com> Date: Sat, 12 Jun 2021 10:36:38 +0200 Subject: [PATCH] Fixed some timer bugs Timer now has a mode for unscaled time FIXED: Countdown now resets properly --- Tools/Timer/Timer.cs | 32 ++++++++++++++++++++++++++------ Tools/Timer/TimerUpdate.cs | 6 ++++++ Tools/Timer/TimerUpdate.cs.meta | 11 +++++++++++ Tools/Timer/TimerUtility.cs | 11 +++++++++-- package.json | 2 +- 5 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 Tools/Timer/TimerUpdate.cs create mode 100644 Tools/Timer/TimerUpdate.cs.meta diff --git a/Tools/Timer/Timer.cs b/Tools/Timer/Timer.cs index 0524682..49a7e28 100644 --- a/Tools/Timer/Timer.cs +++ b/Tools/Timer/Timer.cs @@ -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; + + /// + /// Setup the timer + /// + 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{ } } + /// + /// Play or resume the timer + /// public void Play(){ isPaused = false; StartCoroutine(UpdateTimer()); } + /// + /// Pause the timer + /// public void Stop(){ isPaused = true; } + /// + /// Pause and sets the time to the defaultOne + /// public void ResetTimer(){ isPaused = true; - elapsedTime = 0f; + elapsedTime = defaultTime; timePlaying = TimeSpan.FromSeconds(elapsedTime); timer.text = timePlaying.ToString("m':'ss'.'ff"); } + /// + /// Restarts the timer + /// public void Restart(){ isPaused = false; - elapsedTime = 0f; + elapsedTime = defaultTime; timePlaying = TimeSpan.FromSeconds(elapsedTime); timer.text = timePlaying.ToString("m':'ss'.'ff"); StopAllCoroutines(); diff --git a/Tools/Timer/TimerUpdate.cs b/Tools/Timer/TimerUpdate.cs new file mode 100644 index 0000000..8a95b13 --- /dev/null +++ b/Tools/Timer/TimerUpdate.cs @@ -0,0 +1,6 @@ +namespace SimpleTools.Timer{ + public enum TimerUpdate{ + ScaledTime, + UnscaledTime, + } +} diff --git a/Tools/Timer/TimerUpdate.cs.meta b/Tools/Timer/TimerUpdate.cs.meta new file mode 100644 index 0000000..930e415 --- /dev/null +++ b/Tools/Timer/TimerUpdate.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 977b7b3a05b17c04da63124856e9ff6f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tools/Timer/TimerUtility.cs b/Tools/Timer/TimerUtility.cs index 90ba52e..365e412 100644 --- a/Tools/Timer/TimerUtility.cs +++ b/Tools/Timer/TimerUtility.cs @@ -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){ + /// + /// Setup the timer + /// + /// TMPro object that will contain the timer + /// What type of timer will it be (Countdown, Stopwatch, Clock) + /// The time that will have in case it is a countdown timer + /// + public static Timer SetupTimer(this TMP_Text container, TimerType timerType, TimerUpdate timerUpdate, float countdownTime = 60f){ Timer t = container.gameObject.AddComponent(); 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; } diff --git a/package.json b/package.json index c13a739..f25e252 100644 --- a/package.json +++ b/package.json @@ -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",