Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- using System.Collections;
- /* Example of how to use
- // from rony in #unity3d on Freenode
- // the idea is to reduce the code needed for very simple coroutine cases in c# in Unity3D
- // in a monoBehaviour
- private int hp = 10;
- void Start() {
- //executes the debug.log() after 2 seconds.
- this.ExecuteLater(() => Debug.Log("coucou! 5s"), 2.0f);
- //executes the debug.log() when hp < 5
- this.ExecuteWhen(() => Debug.Log("You lost Life!"), () => hp < 5);
- //executes the given instructions after 6 seconds
- this.ExecuteLater(() => { Debug.Log("changing HP"); hp = 4; }, 6.0f);
- }
- */
- public static class CoroutineExtensions {
- public delegate bool When();
- public static void ExecuteWhen(this MonoBehaviour m, Action action, When condition) {
- m.StartCoroutine(ExecuteWhenCoroutine(action, condition));
- }
- public static void ExecuteLater(this MonoBehaviour m, Action action, float seconds) {
- m.StartCoroutine(ExecuteLaterCoroutine(action, seconds));
- }
- private static IEnumerator ExecuteWhenCoroutine(Action action, When condition) {
- while (!condition()) {
- yield return null;
- }
- action();
- }
- private static IEnumerator ExecuteLaterCoroutine(Action action, float seconds) {
- yield return new WaitForSeconds(seconds);
- action();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement