Unverified Commit a4c26426 authored by Gerard Gascón's avatar Gerard Gascón Committed by GitHub
Browse files

Fixed some timer bugs

Timer now has a mode for unscaled time
FIXED: Countdown now resets properly
parent 750d7f6b
Loading
Loading
Loading
Loading
+26 −6
Original line number Diff line number Diff line
@@ -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();
+6 −0
Original line number Diff line number Diff line
namespace SimpleTools.Timer{
	public enum TimerUpdate{
		ScaledTime,
		UnscaledTime,
	}
}
+11 −0
Original line number Diff line number Diff line
fileFormatVersion: 2
guid: 977b7b3a05b17c04da63124856e9ff6f
MonoImporter:
  externalObjects: {}
  serializedVersion: 2
  defaultReferences: []
  executionOrder: 0
  icon: {instanceID: 0}
  userData: 
  assetBundleName: 
  assetBundleVariant: 
+9 −2
Original line number Diff line number Diff line
@@ -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;
        }
+1 −1
Original line number Diff line number Diff line
{
  "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",