Inherits CustomYieldInstruction.
|
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...
|
|
Run coroutines globally.
Common usages:
- For classes not
UnityEngine.MonoBehaviour
.
- For components on inactive
UnityEngine.GameObject
.
- For static classes or methods.
- For editor classes, or other situation to run in edit mode.
This flattens the coroutine for update stepping to:
- Support to control, i.e.,
Pause()
, Resume()
, and Stop()
.
- Support to run in both play and edit mode.
- 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");
yield return Coroutiner.Start(Nest("Nest"));
yield return StartCoroutine(Nest("Old"));
}
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:
- For edit mode, use
EditorApplication.update
to step.
- For play mode, create a hidden host
UnityEngine.MonoBehaviour
to run.
- Please don't find the host to stop or deactive, otherwise interrupt all coroutines silently.
- All coroutines will be stopped when scripts reloaded or toggle edit and play mode in the editor.