Skip to content

Instantly share code, notes, and snippets.

@TheLouisHong
Created August 2, 2015 11:01
Show Gist options
  • Save TheLouisHong/39323b4a4cd27b498165 to your computer and use it in GitHub Desktop.
Save TheLouisHong/39323b4a4cd27b498165 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class PausableTimer {
private float _timerStart = -1;
private float _timerTime;
public PausableTimer(float initialTimerTime = 0) {
_timerTime = initialTimerTime;
}
public float TimerTime {
get {
if (_timerStart != -1) {
return _timerTime + Time.time - _timerStart;
}
return _timerTime;
}
set { _timerTime = value; }
}
public void StartTimer() {
if (_timerStart == -1) {
_timerStart = Time.time;
}
}
public void PauseTimer() {
if (_timerStart != -1) {
_timerTime += Time.time - _timerStart;
_timerStart = -1;
}
}
public void ResetTimer() {
PauseTimer();
_timerTime = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment