/ Published in: C#
A timer that activates a delegate after a set amount of time. Coded for Unity. This example shows the implementation of an event where all subscribed methods are activated every second. For methods with more elaborate signatures, new delegates will have to be added.
The public IEnumerator can be started from external classes using "StartCoroutine()" and can be passed methods from that class matching the TimerCallbacks delegate signature.
The public IEnumerator can be started from external classes using "StartCoroutine()" and can be passed methods from that class matching the TimerCallbacks delegate signature.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public delegate void TimerCallbacks(); public static event TimerCallbacks EverySecond; void Start () { StartCoroutine(Timer(1f, EverySecond, true)); } public IEnumerator Timer (float timeDelay, TimerCallbacks methodToCall, bool loop = false) { if (loop) { if (methodToCall != null) { methodToCall (); } yield return StartCoroutine (Timer(timeDelay, methodToCall, true)); } else { if (methodToCall != null) { methodToCall (); } yield return null; } }