CinemachineBrainEditor.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. using Cinemachine.Utility;
  5. using System.IO;
  6. namespace Cinemachine.Editor
  7. {
  8. [CustomEditor(typeof(CinemachineBrain))]
  9. [CanEditMultipleObjects]
  10. public sealed class CinemachineBrainEditor : BaseEditor<CinemachineBrain>
  11. {
  12. EmbeddeAssetEditor<CinemachineBlenderSettings> m_BlendsEditor;
  13. bool mEventsExpanded = false;
  14. /// <summary>Obsolete, do not use. Use the overload, which is more performant</summary>
  15. /// <returns>List of property names to exclude</returns>
  16. protected override List<string> GetExcludedPropertiesInInspector()
  17. { return base.GetExcludedPropertiesInInspector(); }
  18. /// <summary>Get the property names to exclude in the inspector.</summary>
  19. /// <param name="excluded">Add the names to this list</param>
  20. protected override void GetExcludedPropertiesInInspector(List<string> excluded)
  21. {
  22. base.GetExcludedPropertiesInInspector(excluded);
  23. excluded.Add(FieldPath(x => x.m_CameraCutEvent));
  24. excluded.Add(FieldPath(x => x.m_CameraActivatedEvent));
  25. excluded.Add(FieldPath(x => x.m_CustomBlends));
  26. }
  27. private void OnEnable()
  28. {
  29. m_BlendsEditor = new EmbeddeAssetEditor<CinemachineBlenderSettings>(FieldPath(x => x.m_CustomBlends), this);
  30. m_BlendsEditor.OnChanged = (CinemachineBlenderSettings b) => { InspectorUtility.RepaintGameView(); };
  31. }
  32. private void OnDisable()
  33. {
  34. if (m_BlendsEditor != null)
  35. m_BlendsEditor.OnDisable();
  36. }
  37. /// <summary>Create the contents of the inspector panel</summary>
  38. public override void OnInspectorGUI()
  39. {
  40. BeginInspector();
  41. // Show the active camera and blend
  42. GUI.enabled = false;
  43. ICinemachineCamera vcam = Target.ActiveVirtualCamera;
  44. Transform activeCam = (vcam != null && vcam.VirtualCameraGameObject != null)
  45. ? vcam.VirtualCameraGameObject.transform : null;
  46. EditorGUILayout.ObjectField("Live Camera", activeCam, typeof(Transform), true);
  47. EditorGUILayout.DelayedTextField(
  48. "Live Blend", Target.ActiveBlend != null
  49. ? Target.ActiveBlend.Description : string.Empty);
  50. GUI.enabled = true;
  51. // Normal properties
  52. DrawRemainingPropertiesInInspector();
  53. if (targets.Length == 1)
  54. {
  55. // Blender
  56. m_BlendsEditor.DrawEditorCombo(
  57. "Create New Blender Asset",
  58. Target.gameObject.name + " Blends", "asset", string.Empty,
  59. "Custom Blends", false);
  60. mEventsExpanded = EditorGUILayout.Foldout(mEventsExpanded, "Events", true);
  61. if (mEventsExpanded)
  62. {
  63. EditorGUILayout.PropertyField(FindProperty(x => x.m_CameraCutEvent));
  64. EditorGUILayout.PropertyField(FindProperty(x => x.m_CameraActivatedEvent));
  65. }
  66. serializedObject.ApplyModifiedProperties();
  67. }
  68. }
  69. [DrawGizmo(GizmoType.Selected | GizmoType.NonSelected, typeof(CinemachineBrain))]
  70. private static void DrawBrainGizmos(CinemachineBrain brain, GizmoType drawType)
  71. {
  72. if (brain.OutputCamera != null && brain.m_ShowCameraFrustum && brain.isActiveAndEnabled)
  73. {
  74. DrawCameraFrustumGizmo(
  75. brain, LensSettings.FromCamera(brain.OutputCamera),
  76. brain.transform.localToWorldMatrix,
  77. Color.white); // GML why is this color hardcoded?
  78. }
  79. }
  80. internal static void DrawCameraFrustumGizmo(
  81. CinemachineBrain brain, LensSettings lens,
  82. Matrix4x4 transform, Color color)
  83. {
  84. float aspect = 1;
  85. bool ortho = false;
  86. if (brain != null)
  87. {
  88. aspect = brain.OutputCamera.aspect;
  89. ortho = brain.OutputCamera.orthographic;
  90. }
  91. Matrix4x4 originalMatrix = Gizmos.matrix;
  92. Color originalGizmoColour = Gizmos.color;
  93. Gizmos.color = color;
  94. Gizmos.matrix = transform;
  95. if (ortho)
  96. {
  97. Vector3 size = new Vector3(
  98. aspect * lens.OrthographicSize * 2,
  99. lens.OrthographicSize * 2,
  100. lens.FarClipPlane - lens.NearClipPlane);
  101. Gizmos.DrawWireCube(
  102. new Vector3(0, 0, (size.z / 2) + lens.NearClipPlane), size);
  103. }
  104. else
  105. {
  106. Gizmos.DrawFrustum(
  107. Vector3.zero, lens.FieldOfView,
  108. lens.FarClipPlane, lens.NearClipPlane, aspect);
  109. }
  110. Gizmos.matrix = originalMatrix;
  111. Gizmos.color = originalGizmoColour;
  112. }
  113. /// <summary>Draw the gizmo for a virtual camera in the scene view</summary>
  114. /// <param name="vcam">The virtual camera</param>
  115. /// <param name="selectionType">How the object is selected</param>
  116. [DrawGizmo(GizmoType.Active | GizmoType.InSelectionHierarchy | GizmoType.Pickable, typeof(CinemachineVirtualCameraBase))]
  117. public static void DrawVirtualCameraBaseGizmos(CinemachineVirtualCameraBase vcam, GizmoType selectionType)
  118. {
  119. // Don't draw gizmos on hidden stuff
  120. if ((vcam.gameObject.hideFlags & (HideFlags.HideInHierarchy | HideFlags.HideInInspector)) != 0)
  121. return;
  122. if (vcam.ParentCamera != null && (selectionType & GizmoType.Active) == 0)
  123. return;
  124. CameraState state = vcam.State;
  125. Gizmos.DrawIcon(state.FinalPosition, kGizmoFileName, true);
  126. DrawCameraFrustumGizmo(
  127. CinemachineCore.Instance.FindPotentialTargetBrain(vcam),
  128. state.Lens,
  129. Matrix4x4.TRS(
  130. state.FinalPosition,
  131. UnityQuaternionExtensions.Normalized(state.FinalOrientation), Vector3.one),
  132. CinemachineCore.Instance.IsLive(vcam)
  133. ? CinemachineSettings.CinemachineCoreSettings.ActiveGizmoColour
  134. : CinemachineSettings.CinemachineCoreSettings.InactiveGizmoColour);
  135. }
  136. #if UNITY_2019_1_OR_NEWER
  137. static string kGizmoFileName = "Packages/com.unity.cinemachine/Gizmos/cm_logo.png";
  138. #else
  139. static string kGizmoFileName = "Cinemachine/cm_logo_lg.png";
  140. [InitializeOnLoad]
  141. static class InstallGizmos
  142. {
  143. static InstallGizmos()
  144. {
  145. string srcFile = ScriptableObjectUtility.CinemachineInstallPath + "/Gizmos/" + kGizmoFileName;
  146. if (File.Exists(srcFile))
  147. {
  148. string dstFile = Application.dataPath + "/Gizmos";
  149. if (!Directory.Exists(dstFile))
  150. Directory.CreateDirectory(dstFile);
  151. dstFile += "/" + kGizmoFileName;
  152. if (!File.Exists(dstFile)
  153. || (File.GetLastWriteTime(dstFile) < File.GetLastWriteTime(srcFile)
  154. && (File.GetAttributes(dstFile) & FileAttributes.ReadOnly) == 0))
  155. {
  156. if (!Directory.Exists(Path.GetDirectoryName(dstFile)))
  157. Directory.CreateDirectory(Path.GetDirectoryName(dstFile));
  158. File.Copy(srcFile, dstFile, true);
  159. File.SetAttributes(
  160. dstFile, File.GetAttributes(dstFile) & ~FileAttributes.ReadOnly);
  161. }
  162. }
  163. }
  164. }
  165. #endif
  166. }
  167. }