TC_FollowTarget.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TerrainComposer2
  4. {
  5. [ExecuteInEditMode]
  6. public class TC_FollowTarget : MonoBehaviour
  7. {
  8. public Transform target;
  9. public Vector3 offset;
  10. public bool refresh = false;
  11. public bool followPosition = true;
  12. public bool followRotation = true;
  13. public bool followScale = true;
  14. public bool followScaleY = true;
  15. Transform t;
  16. TC_ItemBehaviour targetItem;
  17. TC_ItemBehaviour item;
  18. void Start()
  19. {
  20. t = transform;
  21. if (target != null) targetItem = target.GetComponent<TC_ItemBehaviour>();
  22. item = GetComponent<TC_ItemBehaviour>();
  23. }
  24. #if UNITY_EDITOR
  25. void OnEnable()
  26. {
  27. t = transform;
  28. if (target != null) targetItem = target.GetComponent<TC_ItemBehaviour>();
  29. item = GetComponent<TC_ItemBehaviour>();
  30. UnityEditor.EditorApplication.update += Update;
  31. }
  32. void OnDisable()
  33. {
  34. UnityEditor.EditorApplication.update -= Update;
  35. }
  36. #endif
  37. void Update()
  38. {
  39. if (target == null) return;
  40. if (followPosition) t.position = target.position + offset;
  41. if (followRotation) t.rotation = target.rotation;
  42. if (followScale)
  43. {
  44. float scaleY;
  45. if (followScaleY) scaleY = t.localScale.y; else scaleY = target.lossyScale.y;
  46. t.localScale = new Vector3(target.lossyScale.x, scaleY, target.lossyScale.z);
  47. }
  48. if (targetItem != null && item != null)
  49. {
  50. if (item.visible != targetItem.visible)
  51. {
  52. item.visible = targetItem.visible;
  53. TC.RefreshOutputReferences(item.outputId);
  54. }
  55. }
  56. if (refresh)
  57. {
  58. TC.repaintNodeWindow = true;
  59. TC.AutoGenerate();
  60. }
  61. }
  62. }
  63. }