Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using UnityEngine;
- public class AdvancedCoroutines : MonoBehaviour
- {
- IEnumerator Start()
- {
- var isDone = new IsDoneObject();
- StartCoroutine(MyActions.DelayedAction(1, () => print("HUZZAH!"), timeRemaining => print(timeRemaining), isDone));
- while (!isDone) yield return null;
- print("Finished");
- }
- }
- public static class MyActions
- {
- public static IEnumerator DelayedAction(float delayTime, Action action, Action<float> timeRemaingUpdate = null, IsDoneObject isDone = null)
- {
- while (delayTime > 0)
- {
- if (timeRemaingUpdate != null) timeRemaingUpdate(delayTime -= Time.deltaTime);
- yield return null;
- }
- action();
- if (isDone != null) isDone.SetDone();
- }
- }
- public class IsDoneObject
- {
- private bool isDone = false;
- public void SetDone()
- {
- isDone = true;
- }
- public static implicit operator bool(IsDoneObject x)
- {
- return x.isDone;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement