using UnityEngine; using UnityEditor; using Cinemachine.Editor; using System.Collections.Generic; using Cinemachine.Utility; namespace Cinemachine { [CustomEditor(typeof(CinemachineFreeLook))] [CanEditMultipleObjects] internal sealed class CinemachineFreeLookEditor : CinemachineVirtualCameraBaseEditor { /// Get the property names to exclude in the inspector. /// Add the names to this list protected override void GetExcludedPropertiesInInspector(List excluded) { base.GetExcludedPropertiesInInspector(excluded); excluded.Add(FieldPath(x => x.m_Orbits)); if (!Target.m_CommonLens) excluded.Add(FieldPath(x => x.m_Lens)); if (Target.m_BindingMode == CinemachineTransposer.BindingMode.SimpleFollowWithWorldUp) { excluded.Add(FieldPath(x => x.m_Heading)); excluded.Add(FieldPath(x => x.m_RecenterToTargetHeading)); } } protected override void OnEnable() { base.OnEnable(); Target.UpdateInputAxisProvider(); } protected override void OnDisable() { base.OnDisable(); // Must destroy child editors or we get exceptions if (m_rigEditor != null) UnityEngine.Object.DestroyImmediate(m_rigEditor); } public override void OnInspectorGUI() { Target.m_XAxis.ValueRangeLocked = (Target.m_BindingMode == CinemachineTransposer.BindingMode.SimpleFollowWithWorldUp); // Ordinary properties BeginInspector(); DrawHeaderInInspector(); DrawPropertyInInspector(FindProperty(x => x.m_Priority)); DrawTargetsInInspector(FindProperty(x => x.m_Follow), FindProperty(x => x.m_LookAt)); DrawPropertyInInspector(FindProperty(x => x.m_StandbyUpdate)); DrawPropertyInInspector(FindProperty(x => x.m_CommonLens)); DrawLensSettingsInInspector(FindProperty(x => x.m_Lens)); DrawRemainingPropertiesInInspector(); // Orbits EditorGUI.BeginChangeCheck(); SerializedProperty orbits = FindProperty(x => x.m_Orbits); for (int i = 0; i < CinemachineFreeLook.RigNames.Length; ++i) { Rect rect = EditorGUILayout.GetControlRect(true); SerializedProperty orbit = orbits.GetArrayElementAtIndex(i); InspectorUtility.MultiPropertyOnLine(rect, new GUIContent(CinemachineFreeLook.RigNames[i]), new [] { orbit.FindPropertyRelative(() => Target.m_Orbits[i].m_Height), orbit.FindPropertyRelative(() => Target.m_Orbits[i].m_Radius) }, null); } if (EditorGUI.EndChangeCheck()) serializedObject.ApplyModifiedProperties(); // Rigs if (Selection.objects.Length == 1) { EditorGUILayout.Separator(); EditorGUILayout.Separator(); s_SelectedRig = GUILayout.Toolbar(s_SelectedRig, s_RigNames); UpdateRigEditor(); if (m_rigEditor != null) { EditorGUILayout.BeginVertical(GUI.skin.box); ++EditorGUI.indentLevel; m_rigEditor.OnInspectorGUI(); --EditorGUI.indentLevel; EditorGUILayout.EndVertical(); } } // Extensions DrawExtensionsWidgetInInspector(); } static GUIContent[] s_RigNames = { new GUIContent("Top Rig"), new GUIContent("Middle Rig"), new GUIContent("Bottom Rig") }; static int s_SelectedRig = 1; UnityEditor.Editor m_rigEditor; CinemachineVirtualCameraBase m_EditedRig = null; void UpdateRigEditor() { CinemachineVirtualCamera rig = Target.GetRig(s_SelectedRig); if (m_EditedRig != rig || m_rigEditor == null) { m_EditedRig = rig; if (m_rigEditor != null) { UnityEngine.Object.DestroyImmediate(m_rigEditor); m_rigEditor = null; } if (rig != null) { Undo.RecordObject(Target, "selected rig"); Target.m_YAxis.Value = s_SelectedRig == 0 ? 1 : (s_SelectedRig == 1 ? 0.5f : 0); CreateCachedEditor(rig, null, ref m_rigEditor); } } } /// /// Register with CinemachineFreeLook to create the pipeline in an undo-friendly manner /// [InitializeOnLoad] class CreateRigWithUndo { static CreateRigWithUndo() { CinemachineFreeLook.CreateRigOverride = (CinemachineFreeLook vcam, string name, CinemachineVirtualCamera copyFrom) => { // Create a new rig with default components GameObject go = InspectorUtility.CreateGameObject(name); Undo.RegisterCreatedObjectUndo(go, "created rig"); Undo.SetTransformParent(go.transform, vcam.transform, "parenting rig"); CinemachineVirtualCamera rig = Undo.AddComponent(go); Undo.RecordObject(rig, "creating rig"); if (copyFrom != null) ReflectionHelpers.CopyFields(copyFrom, rig); else { go = rig.GetComponentOwner().gameObject; Undo.RecordObject(Undo.AddComponent(go), "creating rig"); Undo.RecordObject(Undo.AddComponent(go), "creating rig"); } return rig; }; CinemachineFreeLook.DestroyRigOverride = (GameObject rig) => { Undo.DestroyObjectImmediate(rig); }; } } [DrawGizmo(GizmoType.Active | GizmoType.Selected, typeof(CinemachineFreeLook))] private static void DrawFreeLookGizmos(CinemachineFreeLook vcam, GizmoType selectionType) { // Standard frustum and logo CinemachineBrainEditor.DrawVirtualCameraBaseGizmos(vcam, selectionType); Color originalGizmoColour = Gizmos.color; bool isActiveVirtualCam = CinemachineCore.Instance.IsLive(vcam); Gizmos.color = isActiveVirtualCam ? CinemachineSettings.CinemachineCoreSettings.ActiveGizmoColour : CinemachineSettings.CinemachineCoreSettings.InactiveGizmoColour; if (vcam.Follow != null) { Vector3 pos = vcam.Follow.position; Vector3 up = vcam.State.ReferenceUp; var MiddleRig = vcam.GetRig(1).GetCinemachineComponent(); if (MiddleRig != null) { Quaternion orient = MiddleRig.GetReferenceOrientation(up); up = orient * Vector3.up; float rotation = vcam.m_XAxis.Value + vcam.m_Heading.m_Bias; orient = Quaternion.AngleAxis(rotation, up) * orient; CinemachineOrbitalTransposerEditor.DrawCircleAtPointWithRadius( pos + up * vcam.m_Orbits[0].m_Height, orient, vcam.m_Orbits[0].m_Radius); CinemachineOrbitalTransposerEditor.DrawCircleAtPointWithRadius( pos + up * vcam.m_Orbits[1].m_Height, orient, vcam.m_Orbits[1].m_Radius); CinemachineOrbitalTransposerEditor.DrawCircleAtPointWithRadius( pos + up * vcam.m_Orbits[2].m_Height, orient, vcam.m_Orbits[2].m_Radius); DrawCameraPath(pos, orient, vcam); } } Gizmos.color = originalGizmoColour; } private static void DrawCameraPath(Vector3 atPos, Quaternion orient, CinemachineFreeLook vcam) { Matrix4x4 prevMatrix = Gizmos.matrix; Gizmos.matrix = Matrix4x4.TRS(atPos, orient, Vector3.one); const int kNumSteps = 20; Vector3 currPos = vcam.GetLocalPositionForCameraFromInput(0f); for (int i = 1; i < kNumSteps + 1; ++i) { float t = (float)i / (float)kNumSteps; Vector3 nextPos = vcam.GetLocalPositionForCameraFromInput(t); Gizmos.DrawLine(currPos, nextPos); currPos = nextPos; } Gizmos.matrix = prevMatrix; } } }