Last active
September 7, 2021 07:44
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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