Skip to content

Instantly share code, notes, and snippets.

@simon-heinen
Last active August 6, 2016 10:35
Show Gist options
  • Save simon-heinen/091808eb97cf42757afdf7653670e21d to your computer and use it in GitHub Desktop.
Save simon-heinen/091808eb97cf42757afdf7653670e21d to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
public class TimerCountdown {
/// <summary>
/// Creates a timer which will return true every x seconds. Usage example in a MonoBehaviour:
///
/// private Func<bool> onTimerRestart = TimerCountDown.newTimeCounter(0.1f); // fire every 100ms
/// ...
/// void Update() {
/// if (!onTimerRestart()) { return; }
///
/// </summary>
/// <param name="countdownInSec">The interval after which the timer resets</param>
/// <returns></returns>
internal static Func<bool> newTimeCounter(float countdownInSec) {
var timer = 0f;
return delegate {
timer += Time.deltaTime;
if (timer < countdownInSec) { return false; }
timer = 0;
return true;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment