Skip to content

Instantly share code, notes, and snippets.

@FlaShG
Last active September 7, 2021 07:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save FlaShG/5fff72a5c4b92f6c460bce341952bce9 to your computer and use it in GitHub Desktop.
Save FlaShG/5fff72a5c4b92f6c460bce341952bce9 to your computer and use it in GitHub Desktop.
Based on MyStaticCode.cs, a class that allows anything to run coroutines from anywhere.
using UnityEngine;
using System.Collections;
public static class CoroutineWorker
{
private class Worker : MonoBehaviour { }
private static Worker worker;
[RuntimeInitializeOnLoadMethod]
private static void Initialize()
{
var go = new GameObject("Coroutine Worker");
worker = go.AddComponent<Worker>();
go.hideFlags = HideFlags.HideAndDontSave;
Object.DontDestroyOnLoad(go);
}
public static Coroutine StartCoroutine(IEnumerator routine)
{
return worker.StartCoroutine(routine);
}
public static void StopCoroutine(Coroutine routine)
{
worker.StopCoroutine(routine);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment