Advertisement
NPSF3000

IsDone Coroutine Example

Sep 30th, 2012
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4.  
  5. public class AdvancedCoroutines : MonoBehaviour
  6. {
  7.     IEnumerator Start()
  8.     {
  9.         var isDone = new IsDoneObject();
  10.         StartCoroutine(MyActions.DelayedAction(1, () => print("HUZZAH!"), timeRemaining => print(timeRemaining), isDone));
  11.         while (!isDone) yield return null;
  12.         print("Finished");
  13.     }
  14. }
  15.  
  16. public static class MyActions
  17. {
  18.     public static IEnumerator DelayedAction(float delayTime, Action action, Action<float> timeRemaingUpdate = null, IsDoneObject isDone = null)
  19.     {
  20.         while (delayTime > 0)
  21.         {
  22.             if (timeRemaingUpdate != null) timeRemaingUpdate(delayTime -= Time.deltaTime);
  23.             yield return null;
  24.         }
  25.  
  26.         action();
  27.         if (isDone != null) isDone.SetDone();
  28.     }
  29. }
  30.  
  31. public class IsDoneObject
  32. {
  33.     private bool isDone = false;
  34.  
  35.     public void SetDone()
  36.     {
  37.         isDone = true;
  38.     }
  39.  
  40.     public static implicit operator bool(IsDoneObject x)
  41.     {
  42.         return x.isDone;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement