Skip to content

Instantly share code, notes, and snippets.

@chris-hatton
Last active July 18, 2016 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-hatton/a58de6cad8939c241fa0cd1ba607194f to your computer and use it in GitHub Desktop.
Save chris-hatton/a58de6cad8939c241fa0cd1ba607194f to your computer and use it in GitHub Desktop.
Adds a better co-routine + delegate based Invoke to MonoBehaviour
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