EditorCoroutine.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. // using UnityEditor;
  5. using UnityEngine;
  6. using Object = UnityEngine.Object;
  7. namespace TerrainComposer2
  8. {
  9. public class EditorCoroutine
  10. {
  11. public bool pause;
  12. public static EditorCoroutine Start(IEnumerator _routine)
  13. {
  14. EditorCoroutine coroutine = new EditorCoroutine(_routine);
  15. coroutine.Start();
  16. return coroutine;
  17. }
  18. readonly IEnumerator routine;
  19. EditorCoroutine(IEnumerator _routine)
  20. {
  21. routine = _routine;
  22. }
  23. void Start()
  24. {
  25. //Debug.Log("start");
  26. #if UNITY_EDITOR
  27. UnityEditor.EditorApplication.update += Update;
  28. #endif
  29. }
  30. public void Stop()
  31. {
  32. //Debug.Log("stop");
  33. #if UNITY_EDITOR
  34. UnityEditor.EditorApplication.update -= Update;
  35. #endif
  36. }
  37. void Update()
  38. {
  39. /* NOTE: no need to try/catch MoveNext,
  40. * if an IEnumerator throws its next iteration returns false.
  41. * Also, Unity probably catches when calling EditorApplication.update.
  42. */
  43. //Debug.Log("update");
  44. if (pause) return;
  45. if (!routine.MoveNext()) Stop();
  46. }
  47. }
  48. }