CoroutineManager.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Collections;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace I2.Loc
  5. {
  6. // This class is used to spawn coroutines from outside of MonoBehaviors
  7. public class CoroutineManager : MonoBehaviour
  8. {
  9. static CoroutineManager pInstance
  10. {
  11. get{
  12. if (mInstance==null)
  13. {
  14. GameObject GO = new GameObject( "_Coroutiner" );
  15. GO.hideFlags = HideFlags.HideAndDontSave;
  16. mInstance = GO.AddComponent<CoroutineManager>();
  17. if (Application.isPlaying)
  18. DontDestroyOnLoad(GO);
  19. }
  20. return mInstance;
  21. }
  22. }
  23. static CoroutineManager mInstance;
  24. private void Awake()
  25. {
  26. if (Application.isPlaying)
  27. DontDestroyOnLoad(gameObject);
  28. }
  29. public static Coroutine Start(IEnumerator coroutine)
  30. {
  31. #if UNITY_EDITOR
  32. // Special case to allow coroutines to run in the Editor
  33. if (!Application.isPlaying)
  34. {
  35. EditorApplication.CallbackFunction delg=null;
  36. delg = delegate
  37. {
  38. if (!coroutine.MoveNext())
  39. EditorApplication.update -= delg;
  40. };
  41. EditorApplication.update += delg;
  42. return null;
  43. }
  44. #endif
  45. return pInstance.StartCoroutine(coroutine);
  46. }
  47. }
  48. }