Created
April 7, 2019 09:10
-
-
Save qapquiz/238c519437f5038e1fff8c3a0075c402 to your computer and use it in GitHub Desktop.
This class allows us to start Coroutines from non-Monobehaviour scripts
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; | |
/// <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