Skip to content

Instantly share code, notes, and snippets.

@CSaratakij
Last active September 9, 2015 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CSaratakij/d92e0a80eecd1f556d48 to your computer and use it in GitHub Desktop.
Save CSaratakij/d92e0a80eecd1f556d48 to your computer and use it in GitHub Desktop.
Unity Timer Example (Author : Chatchai Saratakij (Ton))
using UnityEngine;
using System.Collections;
public sealed class Timer : MonoBehaviour {
[SerializeField]
[Range(0, 600)]
[Tooltip("Time in seconds. (Time between 0 seconds to 10 minutes.)")]
int time;
int minuteTime;
int secondsTime;
bool isStarted;
bool isTimesUp;
public int CurrentTime { get { return time; } }
public int CurrentTimeInMinute { get { return minuteTime; } }
public int CurrentTimeInSeconds { get { return secondsTime; } }
public bool IsStarted { get { return isStarted; } }
public bool IsTimesUp { get { return isTimesUp; } }
public Timer() {
time = 60;
minuteTime = time / 60;
secondsTime = time % 60;
isStarted = isTimesUp = false;
}
void Start() {
StartCoroutine("Countdown");
}
IEnumerator Countdown () {
while (!isTimesUp) {
if (isStarted) {
time = (time > 0) ? time - 1 : 0;
minuteTime = time / 60;
secondsTime = time % 60;
isTimesUp = (time == 0) ? true : false;
}
yield return new WaitForSeconds(1f);
}
}
public void SetStart(bool isStart) {
isStarted = isStart;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment