Coroutiner - edcasillas/unity-common-utils GitHub Wiki

A utility static class that allows executing coroutines from static classes, POCO objects and disabled game objects.

Original credit to: Sebastiaan Fehr ([email protected])

Original repository: https://github.com/Fehrox/Coroutiner


Classes that do not inherit from MonoBehaviour, or static methods within MonoBehaviours are inertly unable to call StartCoroutine, as this method is not static and does not exist on Object. This class creates a proxy through which StartCoroutine can be called.

This version of the Coroutiner keeps a pool of the instances it has created so they can be reused on subsequent calls to Coroutiner.StartCoroutine instead of creating and destroying objects on each call. Instances in this pool are destroyed when the scene is unloaded, which means that any running coroutine will be stopped. This behavior can be modified by sending a true value for the preventDestroyOnSceneChange parameter, which will mark the coroutiner instance as DontDestroyOnLoad, making it able to continue running the coroutine when the next scene is loaded.


Example

public class MyPoco {
    public void CallCoroutine() {
        Coroutiner.StartCoroutine(myCoroutine());
    }

    private IEnumerator myCoroutine() {
        yield return null;
    }
}