Last active
September 9, 2015 16:54
-
-
Save CSaratakij/d92e0a80eecd1f556d48 to your computer and use it in GitHub Desktop.
Unity Timer Example (Author : Chatchai Saratakij (Ton))
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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