TC_AutoGenerate.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TerrainComposer2
  4. {
  5. [ExecuteInEditMode]
  6. public class TC_AutoGenerate : MonoBehaviour
  7. {
  8. [HideInInspector] public CachedTransform cT = new CachedTransform();
  9. public bool generateOnEnable = true;
  10. public bool generateOnDisable = true;
  11. public bool instantGenerate;
  12. public bool waitForEndOfFrame;
  13. bool generate;
  14. Transform t;
  15. // public bool repeat;
  16. void Start()
  17. {
  18. t = transform;
  19. cT.Copy(t);
  20. }
  21. #if !UNITY_EDITOR
  22. void Update()
  23. {
  24. MyUpdate();
  25. }
  26. #endif
  27. void MyUpdate()
  28. {
  29. // if (repeat) TC.AutoGenerate();
  30. if (cT.hasChanged(t))
  31. {
  32. // Debug.Log("Auto generate");
  33. cT.Copy(t);
  34. if (waitForEndOfFrame) generate = true; else Generate();
  35. }
  36. }
  37. void LateUpdate()
  38. {
  39. if (generate) Generate();
  40. }
  41. void Generate()
  42. {
  43. generate = false;
  44. if (instantGenerate) TC_Generate.instance.Generate(true);
  45. else TC.AutoGenerate();
  46. }
  47. void OnEnable()
  48. {
  49. if (generateOnEnable) TC.AutoGenerate();
  50. #if UNITY_EDITOR
  51. UnityEditor.EditorApplication.update += MyUpdate;
  52. #endif
  53. }
  54. void OnDisable()
  55. {
  56. if (generateOnDisable) TC.AutoGenerate();
  57. #if UNITY_EDITOR
  58. UnityEditor.EditorApplication.update -= MyUpdate;
  59. #endif
  60. }
  61. void OnDestroy()
  62. {
  63. #if UNITY_EDITOR
  64. UnityEditor.EditorApplication.update -= MyUpdate;
  65. #endif
  66. }
  67. }
  68. }