CinemachineBrainEditor.cs 7.8 KB

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