CinemachineOrbitalTransposerEditor.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. namespace Cinemachine.Editor
  5. {
  6. [CustomEditor(typeof(CinemachineOrbitalTransposer))]
  7. [CanEditMultipleObjects]
  8. internal class CinemachineOrbitalTransposerEditor : BaseEditor<CinemachineOrbitalTransposer>
  9. {
  10. /// <summary>Get the property names to exclude in the inspector.</summary>
  11. /// <param name="excluded">Add the names to this list</param>
  12. protected override void GetExcludedPropertiesInInspector(List<string> excluded)
  13. {
  14. base.GetExcludedPropertiesInInspector(excluded);
  15. if (Target.m_HeadingIsSlave)
  16. {
  17. excluded.Add(FieldPath(x => x.m_BindingMode));
  18. excluded.Add(FieldPath(x => x.m_Heading));
  19. excluded.Add(FieldPath(x => x.m_XAxis));
  20. excluded.Add(FieldPath(x => x.m_RecenterToTargetHeading));
  21. }
  22. if (Target.HideOffsetInInspector)
  23. excluded.Add(FieldPath(x => x.m_FollowOffset));
  24. switch (Target.m_BindingMode)
  25. {
  26. default:
  27. case CinemachineTransposer.BindingMode.LockToTarget:
  28. if (Target.m_AngularDampingMode == CinemachineTransposer.AngularDampingMode.Euler)
  29. excluded.Add(FieldPath(x => x.m_AngularDamping));
  30. else
  31. {
  32. excluded.Add(FieldPath(x => x.m_PitchDamping));
  33. excluded.Add(FieldPath(x => x.m_YawDamping));
  34. excluded.Add(FieldPath(x => x.m_RollDamping));
  35. }
  36. break;
  37. case CinemachineTransposer.BindingMode.LockToTargetNoRoll:
  38. excluded.Add(FieldPath(x => x.m_RollDamping));
  39. excluded.Add(FieldPath(x => x.m_AngularDamping));
  40. excluded.Add(FieldPath(x => x.m_AngularDampingMode));
  41. break;
  42. case CinemachineTransposer.BindingMode.LockToTargetWithWorldUp:
  43. excluded.Add(FieldPath(x => x.m_PitchDamping));
  44. excluded.Add(FieldPath(x => x.m_RollDamping));
  45. excluded.Add(FieldPath(x => x.m_AngularDamping));
  46. excluded.Add(FieldPath(x => x.m_AngularDampingMode));
  47. break;
  48. case CinemachineTransposer.BindingMode.LockToTargetOnAssign:
  49. case CinemachineTransposer.BindingMode.WorldSpace:
  50. excluded.Add(FieldPath(x => x.m_PitchDamping));
  51. excluded.Add(FieldPath(x => x.m_YawDamping));
  52. excluded.Add(FieldPath(x => x.m_RollDamping));
  53. excluded.Add(FieldPath(x => x.m_AngularDamping));
  54. excluded.Add(FieldPath(x => x.m_AngularDampingMode));
  55. break;
  56. case CinemachineTransposer.BindingMode.SimpleFollowWithWorldUp:
  57. excluded.Add(FieldPath(x => x.m_XDamping));
  58. excluded.Add(FieldPath(x => x.m_PitchDamping));
  59. excluded.Add(FieldPath(x => x.m_YawDamping));
  60. excluded.Add(FieldPath(x => x.m_RollDamping));
  61. excluded.Add(FieldPath(x => x.m_AngularDamping));
  62. excluded.Add(FieldPath(x => x.m_AngularDampingMode));
  63. excluded.Add(FieldPath(x => x.m_Heading));
  64. excluded.Add(FieldPath(x => x.m_RecenterToTargetHeading));
  65. break;
  66. }
  67. }
  68. public override void OnInspectorGUI()
  69. {
  70. BeginInspector();
  71. bool needWarning = false;
  72. for (int i = 0; !needWarning && i < targets.Length; ++i)
  73. needWarning = (targets[i] as CinemachineOrbitalTransposer).FollowTarget == null;
  74. if (needWarning)
  75. EditorGUILayout.HelpBox(
  76. "Orbital Transposer requires a Follow target.",
  77. MessageType.Warning);
  78. Target.m_XAxis.ValueRangeLocked
  79. = (Target.m_BindingMode == CinemachineTransposer.BindingMode.SimpleFollowWithWorldUp);
  80. DrawRemainingPropertiesInInspector();
  81. }
  82. [DrawGizmo(GizmoType.Active | GizmoType.Selected, typeof(CinemachineOrbitalTransposer))]
  83. static void DrawTransposerGizmos(CinemachineOrbitalTransposer target, GizmoType selectionType)
  84. {
  85. if (target.IsValid && !target.HideOffsetInInspector)
  86. {
  87. Color originalGizmoColour = Gizmos.color;
  88. Gizmos.color = CinemachineCore.Instance.IsLive(target.VirtualCamera)
  89. ? CinemachineSettings.CinemachineCoreSettings.ActiveGizmoColour
  90. : CinemachineSettings.CinemachineCoreSettings.InactiveGizmoColour;
  91. Vector3 up = target.VirtualCamera.State.ReferenceUp;
  92. Vector3 pos = target.FollowTargetPosition;
  93. Quaternion orient = target.GetReferenceOrientation(up);
  94. up = orient * Vector3.up;
  95. DrawCircleAtPointWithRadius
  96. (pos + up * target.m_FollowOffset.y, orient, target.m_FollowOffset.z);
  97. Gizmos.color = originalGizmoColour;
  98. }
  99. }
  100. public static void DrawCircleAtPointWithRadius(Vector3 point, Quaternion orient, float radius)
  101. {
  102. Matrix4x4 prevMatrix = Gizmos.matrix;
  103. Gizmos.matrix = Matrix4x4.TRS(point, orient, radius * Vector3.one);
  104. const int kNumPoints = 25;
  105. Vector3 currPoint = Vector3.forward;
  106. Quaternion rot = Quaternion.AngleAxis(360f / (float)kNumPoints, Vector3.up);
  107. for (int i = 0; i < kNumPoints + 1; ++i)
  108. {
  109. Vector3 nextPoint = rot * currPoint;
  110. Gizmos.DrawLine(currPoint, nextPoint);
  111. currPoint = nextPoint;
  112. }
  113. Gizmos.matrix = prevMatrix;
  114. }
  115. protected virtual void OnEnable()
  116. {
  117. for (int i = 0; i < targets.Length; ++i)
  118. (targets[i] as CinemachineOrbitalTransposer).UpdateInputAxisProvider();
  119. #if UNITY_2021_2_OR_NEWER
  120. if (!Target.HideOffsetInInspector)
  121. {
  122. CinemachineSceneToolUtility.RegisterTool(typeof(FollowOffsetTool));
  123. }
  124. #endif
  125. }
  126. protected virtual void OnDisable()
  127. {
  128. #if UNITY_2021_2_OR_NEWER
  129. if (!Target.HideOffsetInInspector)
  130. {
  131. CinemachineSceneToolUtility.UnregisterTool(typeof(FollowOffsetTool));
  132. }
  133. #endif
  134. }
  135. #if UNITY_2021_2_OR_NEWER
  136. void OnSceneGUI()
  137. {
  138. DrawSceneTools();
  139. }
  140. void DrawSceneTools()
  141. {
  142. var orbitalTransposer = Target;
  143. if (orbitalTransposer == null || !orbitalTransposer.IsValid || orbitalTransposer.HideOffsetInInspector)
  144. {
  145. return;
  146. }
  147. if (CinemachineSceneToolUtility.IsToolActive(typeof(FollowOffsetTool)))
  148. {
  149. CinemachineSceneToolHelpers.TransposerFollowOffsetTool(orbitalTransposer);
  150. }
  151. }
  152. #endif
  153. }
  154. }