CinemachineTransposerEditor.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. namespace Cinemachine.Editor
  5. {
  6. [CustomEditor(typeof(CinemachineTransposer))]
  7. internal sealed class CinemachineTransposerEditor : BaseEditor<CinemachineTransposer>
  8. {
  9. /// <summary>Get the property names to exclude in the inspector.</summary>
  10. /// <param name="excluded">Add the names to this list</param>
  11. protected override void GetExcludedPropertiesInInspector(List<string> excluded)
  12. {
  13. base.GetExcludedPropertiesInInspector(excluded);
  14. switch (Target.m_BindingMode)
  15. {
  16. default:
  17. case CinemachineTransposer.BindingMode.LockToTarget:
  18. if (Target.m_AngularDampingMode == CinemachineTransposer.AngularDampingMode.Euler)
  19. excluded.Add(FieldPath(x => x.m_AngularDamping));
  20. else
  21. {
  22. excluded.Add(FieldPath(x => x.m_PitchDamping));
  23. excluded.Add(FieldPath(x => x.m_YawDamping));
  24. excluded.Add(FieldPath(x => x.m_RollDamping));
  25. }
  26. break;
  27. case CinemachineTransposer.BindingMode.LockToTargetNoRoll:
  28. excluded.Add(FieldPath(x => x.m_RollDamping));
  29. excluded.Add(FieldPath(x => x.m_AngularDamping));
  30. excluded.Add(FieldPath(x => x.m_AngularDampingMode));
  31. break;
  32. case CinemachineTransposer.BindingMode.LockToTargetWithWorldUp:
  33. excluded.Add(FieldPath(x => x.m_PitchDamping));
  34. excluded.Add(FieldPath(x => x.m_RollDamping));
  35. excluded.Add(FieldPath(x => x.m_AngularDamping));
  36. excluded.Add(FieldPath(x => x.m_AngularDampingMode));
  37. break;
  38. case CinemachineTransposer.BindingMode.LockToTargetOnAssign:
  39. case CinemachineTransposer.BindingMode.WorldSpace:
  40. excluded.Add(FieldPath(x => x.m_PitchDamping));
  41. excluded.Add(FieldPath(x => x.m_YawDamping));
  42. excluded.Add(FieldPath(x => x.m_RollDamping));
  43. excluded.Add(FieldPath(x => x.m_AngularDamping));
  44. excluded.Add(FieldPath(x => x.m_AngularDampingMode));
  45. break;
  46. case CinemachineTransposer.BindingMode.SimpleFollowWithWorldUp:
  47. excluded.Add(FieldPath(x => x.m_XDamping));
  48. excluded.Add(FieldPath(x => x.m_PitchDamping));
  49. excluded.Add(FieldPath(x => x.m_YawDamping));
  50. excluded.Add(FieldPath(x => x.m_RollDamping));
  51. excluded.Add(FieldPath(x => x.m_AngularDamping));
  52. excluded.Add(FieldPath(x => x.m_AngularDampingMode));
  53. break;
  54. }
  55. if (Target.HideOffsetInInspector)
  56. excluded.Add(FieldPath(x => x.m_FollowOffset));
  57. }
  58. public override void OnInspectorGUI()
  59. {
  60. BeginInspector();
  61. if (Target.FollowTarget == null)
  62. EditorGUILayout.HelpBox(
  63. "Transposer requires a Follow Target. Change Body to Do Nothing if you don't want a Follow target.",
  64. MessageType.Warning);
  65. DrawRemainingPropertiesInInspector();
  66. }
  67. /// Process a position drag from the user.
  68. /// Called "magically" by the vcam editor, so don't change the signature.
  69. public void OnVcamPositionDragged(Vector3 delta)
  70. {
  71. if (Target.FollowTarget != null)
  72. {
  73. Undo.RegisterCompleteObjectUndo(Target, "Camera drag");
  74. Quaternion targetOrientation = Target.GetReferenceOrientation(Target.VcamState.ReferenceUp);
  75. Vector3 localOffset = Quaternion.Inverse(targetOrientation) * delta;
  76. Target.m_FollowOffset += localOffset;
  77. Target.m_FollowOffset = Target.EffectiveOffset;
  78. }
  79. }
  80. [DrawGizmo(GizmoType.Active | GizmoType.Selected, typeof(CinemachineTransposer))]
  81. static void DrawTransposerGizmos(CinemachineTransposer target, GizmoType selectionType)
  82. {
  83. if (target.IsValid & !target.HideOffsetInInspector)
  84. {
  85. Color originalGizmoColour = Gizmos.color;
  86. Gizmos.color = CinemachineCore.Instance.IsLive(target.VirtualCamera)
  87. ? CinemachineSettings.CinemachineCoreSettings.ActiveGizmoColour
  88. : CinemachineSettings.CinemachineCoreSettings.InactiveGizmoColour;
  89. Vector3 up = Vector3.up;
  90. CinemachineBrain brain = CinemachineCore.Instance.FindPotentialTargetBrain(target.VirtualCamera);
  91. if (brain != null)
  92. up = brain.DefaultWorldUp;
  93. Vector3 targetPos = target.FollowTargetPosition;
  94. Vector3 desiredPos = target.GetTargetCameraPosition(up);
  95. Gizmos.DrawLine(targetPos, desiredPos);
  96. //Gizmos.DrawWireSphere(desiredPos, HandleUtility.GetHandleSize(desiredPos) / 20);
  97. Gizmos.color = originalGizmoColour;
  98. }
  99. }
  100. }
  101. }