Added Timer
New version featuring a timer
This commit is contained in:
parent
f7c4067db4
commit
750d7f6b88
14 changed files with 186 additions and 23 deletions
79
Tools/Timer/Timer.cs
Normal file
79
Tools/Timer/Timer.cs
Normal file
|
@ -0,0 +1,79 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using TMPro;
|
||||
|
||||
namespace SimpleTools.Timer{
|
||||
public class Timer : MonoBehaviour{
|
||||
|
||||
float elapsedTime;
|
||||
public float ElapsedTime { get { return elapsedTime; } }
|
||||
bool isPaused;
|
||||
public bool IsPaused { get { return isPaused; } }
|
||||
TimeSpan timePlaying;
|
||||
public TimeSpan TimePlaying { get { return timePlaying; } }
|
||||
TMP_Text timer;
|
||||
public TMP_Text TimerText { get { return timer; } }
|
||||
TimerType timerType;
|
||||
public TimerType TimerType { get { return timerType; } }
|
||||
|
||||
public void Setup(float elapsedTime, bool isPaused, TimeSpan timePlaying, TMP_Text timer, TimerType timerType, string text){
|
||||
this.elapsedTime = elapsedTime;
|
||||
this.isPaused = isPaused;
|
||||
this.timePlaying = timePlaying;
|
||||
this.timer = timer;
|
||||
this.timerType = timerType;
|
||||
timer.text = text;
|
||||
}
|
||||
|
||||
IEnumerator UpdateTimer(){
|
||||
while (!isPaused){
|
||||
if(timerType == TimerType.Clock){
|
||||
timer.text = DateTime.Now.ToString("HH:mm:ss");
|
||||
}else{
|
||||
switch (timerType){
|
||||
case TimerType.Countdown:
|
||||
elapsedTime -= Time.deltaTime;
|
||||
if(elapsedTime < 0f){
|
||||
elapsedTime = 0f;
|
||||
isPaused = true;
|
||||
}
|
||||
break;
|
||||
case TimerType.Stopwatch:
|
||||
elapsedTime += Time.deltaTime;
|
||||
break;
|
||||
}
|
||||
timePlaying = TimeSpan.FromSeconds(elapsedTime);
|
||||
timer.text = timePlaying.ToString("m':'ss'.'ff");
|
||||
}
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Play(){
|
||||
isPaused = false;
|
||||
StartCoroutine(UpdateTimer());
|
||||
}
|
||||
|
||||
public void Stop(){
|
||||
isPaused = true;
|
||||
}
|
||||
|
||||
public void ResetTimer(){
|
||||
isPaused = true;
|
||||
elapsedTime = 0f;
|
||||
timePlaying = TimeSpan.FromSeconds(elapsedTime);
|
||||
timer.text = timePlaying.ToString("m':'ss'.'ff");
|
||||
}
|
||||
|
||||
public void Restart(){
|
||||
isPaused = false;
|
||||
elapsedTime = 0f;
|
||||
timePlaying = TimeSpan.FromSeconds(elapsedTime);
|
||||
timer.text = timePlaying.ToString("m':'ss'.'ff");
|
||||
StopAllCoroutines();
|
||||
StartCoroutine(UpdateTimer());
|
||||
}
|
||||
}
|
||||
}
|
11
Tools/Timer/Timer.cs.meta
Normal file
11
Tools/Timer/Timer.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6daeed0a6935f484f85fc0ec1871344f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
7
Tools/Timer/TimerType.cs
Normal file
7
Tools/Timer/TimerType.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace SimpleTools.Timer{
|
||||
public enum TimerType{
|
||||
Countdown,
|
||||
Stopwatch,
|
||||
Clock
|
||||
}
|
||||
}
|
11
Tools/Timer/TimerType.cs.meta
Normal file
11
Tools/Timer/TimerType.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f285456223780f946b49c6131d493c01
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
29
Tools/Timer/TimerUtility.cs
Normal file
29
Tools/Timer/TimerUtility.cs
Normal file
|
@ -0,0 +1,29 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using TMPro;
|
||||
|
||||
namespace SimpleTools.Timer{
|
||||
public static class TimerUtility {
|
||||
public static Timer SetupTimer(this TMP_Text container, TimerType timerType, float countdownTime = 60f){
|
||||
Timer t = container.gameObject.AddComponent<Timer>();
|
||||
float elapsedTime = 0f;
|
||||
string text = string.Empty;
|
||||
TimeSpan timePlaying = TimeSpan.Zero;
|
||||
switch (timerType){
|
||||
case TimerType.Countdown:
|
||||
elapsedTime = countdownTime;
|
||||
timePlaying = TimeSpan.FromSeconds(elapsedTime);
|
||||
text = timePlaying.ToString("m':'ss'.'ff");
|
||||
break;
|
||||
case TimerType.Clock:
|
||||
text = DateTime.Now.ToString("HH:mm:ss");
|
||||
break;
|
||||
}
|
||||
t.Setup(elapsedTime, true, timePlaying, container, timerType, text);
|
||||
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
11
Tools/Timer/TimerUtility.cs.meta
Normal file
11
Tools/Timer/TimerUtility.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 38076483429281e438cee653b26bb03d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Add table
Add a link
Reference in a new issue