Last active
July 18, 2016 12:57
-
-
Save chris-hatton/a58de6cad8939c241fa0cd1ba607194f to your computer and use it in GitHub Desktop.
Adds a better co-routine + delegate based Invoke to MonoBehaviour
This file contains hidden or 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 System; | |
using System.Collections; | |
using UnityEngine; | |
/* | |
* Use by explicit 'this' e.g. this.Invoke( 3.0f, delegate { doSomething(); } ); | |
*/ | |
static class MonoBehaviourInvokeExtension | |
{ | |
public static void Invoke( this MonoBehaviour monoBehaviour, float delay, Action action ) | |
{ | |
monoBehaviour.StartCoroutine( StartTimer( delay, action ) ); | |
} | |
private static IEnumerator StartTimer( float delay, Action action ) | |
{ | |
float actionTime = Time.realtimeSinceStartup + delay; | |
while (Time.realtimeSinceStartup < actionTime) yield return false; | |
action(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment