Skip to content

Instantly share code, notes, and snippets.

@qapquiz
Created April 7, 2019 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qapquiz/238c519437f5038e1fff8c3a0075c402 to your computer and use it in GitHub Desktop.
Save qapquiz/238c519437f5038e1fff8c3a0075c402 to your computer and use it in GitHub Desktop.
This class allows us to start Coroutines from non-Monobehaviour scripts
using UnityEngine;
using System.Collections;
/// <summary>
/// This class allows us to start Coroutines from non-Monobehaviour scripts
/// Create a GameObject it will use to launch the coroutine on
/// </summary>
public class CoroutineHandler : MonoBehaviour
{
static protected CoroutineHandler m_Instance;
static public CoroutineHandler instance
{
get
{
if(m_Instance == null)
{
GameObject o = new GameObject("CoroutineHandler");
DontDestroyOnLoad(o);
m_Instance = o.AddComponent<CoroutineHandler>();
}
return m_Instance;
}
}
public void OnDisable()
{
if(m_Instance)
Destroy(m_Instance.gameObject);
}
static public Coroutine StartStaticCoroutine(IEnumerator coroutine)
{
return instance.StartCoroutine(coroutine);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment