Last active
August 29, 2015 14:22
-
-
Save wowbroforce/6580140b54eda2f74d8d to your computer and use it in GitHub Desktop.
Simple actions, based on coroutines and callbacks. Delays, loops etc...
This file contains 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 UnityEngine; | |
using System.Collections; | |
using System; | |
public static class CoroutineExtensions { | |
public static void StartWaitForSeconds(this MonoBehaviour target, float seconds, Action completion) { | |
target.StartCoroutine (WaitForSeconds (seconds, completion)); | |
} | |
public static void StartWaitForSeconds(this MonoBehaviour target, float seconds, Action start, Action completion) { | |
target.StartCoroutine (WaitForSeconds (seconds, start, completion)); | |
} | |
public static void StartLoop(this MonoBehaviour target, float interval, Action action) { | |
target.StartCoroutine (Loop (interval, action)); | |
} | |
public static void StartLoop(this MonoBehaviour target, float interval, Func<bool> condition, Action action) { | |
target.StartCoroutine (Loop (interval, condition, action)); | |
} | |
private static IEnumerator WaitForSeconds(float seconds, Action completion) { | |
yield return new WaitForSeconds (seconds); | |
if (completion != null) | |
completion (); | |
} | |
private static IEnumerator WaitForSeconds(float seconds, Action start, Action completion) { | |
if (start != null) | |
start (); | |
yield return new WaitForSeconds (seconds); | |
if (completion != null) | |
completion (); | |
} | |
private static IEnumerator Loop(float interval, Action action) { | |
while (true) { | |
yield return new WaitForSeconds (interval); | |
if (action != null) | |
action (); | |
} | |
} | |
private static IEnumerator Loop(float interval, Func<bool> condition, Action action) { | |
while (condition ()) { | |
yield return new WaitForSeconds (interval); | |
if (action != null) | |
action (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment