Wanzyee Studio

Run coroutines globally. More...

Inherits CustomYieldInstruction.

Public Member Functions

void Pause ()
 Pause the coroutine. More...
 
void Resume ()
 Resume the coroutine. More...
 
void Stop ()
 Stop the coroutine. More...
 

Static Public Member Functions

static Coroutiner Start (IEnumerator routine, Object owner=null)
 Start the coroutine, used to replace MonoBehaviour.StartCoroutine. More...
 
static Coroutiner Delay (Action action, object delay=null, Object owner=null)
 Delay invoke the action, e.g., UnityEngine.WaitForSeconds, or null as a frame, etc. More...
 
static Coroutiner Invoke (Action action, float time, float repeatRate=0f, bool unscaled=false, Object owner=null)
 Invoke the action after a time, optional to set repeat or only once. More...
 

Public Attributes

Func< bool > stopper
 The callback to stop the coroutine, if exists and returns true. More...
 
override bool keepWaiting => !isDone
 Indicates if coroutine should be kept suspended. More...
 

Properties

bool isPaused [get]
 Determine if the coroutine is paused. More...
 
bool isDone [get]
 Determine if the coroutine is done, i.e., it finished properly or stopped manually. More...
 

Detailed Description

Run coroutines globally.

Common usages:

  1. For classes not UnityEngine.MonoBehaviour.
  2. For components on inactive UnityEngine.GameObject.
  3. For static classes or methods.
  4. For editor classes, or other situation to run in edit mode.

This flattens the coroutine for update stepping to:

  1. Support to control, i.e., Pause(), Resume(), and Stop().
  2. Support to run in both play and edit mode.
  3. Support multi-level nested coroutines.

Examples below to use easily, just like UnityEngine.MonoBehaviour, even with nested coroutine.

private void Run(){
Coroutiner.Start(Wait("All", All()));
}
private IEnumerator All(){
yield return Nest("Recommended"); //Easy to use, support control and edit mode.
yield return Coroutiner.Start(Nest("Nest")); //Only if you need specific control.
yield return StartCoroutine(Nest("Old")); //Built-in Coroutine is uncontrollable.
}
private IEnumerator Nest(string label){
yield return Wait(label, new WaitForSeconds(1f));
}
private IEnumerator Wait(string label, object what){
Debug.Log($"Start {label}");
yield return what;
Debug.Log($"End {label}");
}

Examples below to easily Fire'n'Forget repeat or delay invoke:

Coroutiner.Invoke(() => Debug.Log(Time.time), 1f, 1f);
Coroutiner.Delay(() => Debug.Log("Delayed"), new WaitForSeconds(2f));
Coroutiner.Delay(() => Debug.Log("Delayed"), Nest("Delay"));

Examples below to auto stop, e.g., when destroy the owner, or unfocus the window:

Coroutiner.Start(All(), gameObject);
Coroutiner.Start(All()).stopper = () => this != EditorWindow.focusedWindow;

The mechanism under the hood:

  1. For edit mode, use EditorApplication.update to step.
  2. For play mode, create a hidden host UnityEngine.MonoBehaviour to run.
  3. Please don't find the host to stop or deactive, otherwise interrupt all coroutines silently.
  4. All coroutines will be stopped when scripts reloaded or toggle edit and play mode in the editor.

Member Function Documentation

static Coroutiner Start ( IEnumerator  routine,
Object  owner = null 
)
static

Start the coroutine, used to replace MonoBehaviour.StartCoroutine.

Parameters
routineRoutine to run.
ownerSet the stopper to check if to stop when the owner is destroyed.
Returns
The running Coroutiner.
static Coroutiner Delay ( Action  action,
object  delay = null,
Object  owner = null 
)
static

Delay invoke the action, e.g., UnityEngine.WaitForSeconds, or null as a frame, etc.

Parameters
actionAction to invoke.
delayThe delay objcet to yield return.
ownerSet the stopper to check if to stop when the owner is destroyed.
Returns
The running Coroutiner.
static Coroutiner Invoke ( Action  action,
float  time,
float  repeatRate = 0f,
bool  unscaled = false,
Object  owner = null 
)
static

Invoke the action after a time, optional to set repeat or only once.

Used to replace MonoBehaviour.Invoke and MonoBehaviour.InvokeRepeating. The time parameters are in seconds, the default zero repeatRate means only once. To stop repeating, just call Stop() on the returned instance.

Parameters
actionAction to invoke.
timeStart time.
repeatRateRepeat interval.
unscaledUse unscaled time.
ownerSet the stopper to check if to stop when the owner is destroyed.
Returns
The running Coroutiner.
void Pause ( )

Pause the coroutine.

void Resume ( )

Resume the coroutine.

void Stop ( )

Stop the coroutine.

Member Data Documentation

Func<bool> stopper

The callback to stop the coroutine, if exists and returns true.

override bool keepWaiting => !isDone

Indicates if coroutine should be kept suspended.

Property Documentation

bool isPaused
get

Determine if the coroutine is paused.

bool isDone
get

Determine if the coroutine is done, i.e., it finished properly or stopped manually.