CinemachineBlendListCameraEditor.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace Cinemachine.Editor
  5. {
  6. [CustomEditor(typeof(CinemachineBlendListCamera))]
  7. [CanEditMultipleObjects]
  8. internal sealed class CinemachineBlendListCameraEditor
  9. : CinemachineVirtualCameraBaseEditor<CinemachineBlendListCamera>
  10. {
  11. /// <summary>Get the property names to exclude in the inspector.</summary>
  12. /// <param name="excluded">Add the names to this list</param>
  13. protected override void GetExcludedPropertiesInInspector(List<string> excluded)
  14. {
  15. base.GetExcludedPropertiesInInspector(excluded);
  16. excluded.Add(FieldPath(x => x.m_Instructions));
  17. }
  18. private UnityEditorInternal.ReorderableList mChildList;
  19. private UnityEditorInternal.ReorderableList mInstructionList;
  20. protected override void OnEnable()
  21. {
  22. base.OnEnable();
  23. mChildList = null;
  24. mInstructionList = null;
  25. }
  26. protected override void OnDisable()
  27. {
  28. base.OnDisable();
  29. }
  30. public override void OnInspectorGUI()
  31. {
  32. BeginInspector();
  33. if (mInstructionList == null)
  34. SetupInstructionList();
  35. if (mChildList == null)
  36. SetupChildList();
  37. // Ordinary properties
  38. DrawHeaderInInspector();
  39. DrawPropertyInInspector(FindProperty(x => x.m_Priority));
  40. DrawTargetsInInspector(FindProperty(x => x.m_Follow), FindProperty(x => x.m_LookAt));
  41. DrawRemainingPropertiesInInspector();
  42. if (targets.Length == 1)
  43. {
  44. // Instructions
  45. UpdateCameraCandidates();
  46. EditorGUI.BeginChangeCheck();
  47. EditorGUILayout.Separator();
  48. mInstructionList.DoLayoutList();
  49. EditorGUILayout.Separator();
  50. mChildList.DoLayoutList();
  51. if (EditorGUI.EndChangeCheck())
  52. {
  53. serializedObject.ApplyModifiedProperties();
  54. Target.ValidateInstructions();
  55. }
  56. }
  57. else
  58. {
  59. EditorGUILayout.HelpBox(Styles.virtualCameraChildrenInfoMsg.text, MessageType.Info);
  60. }
  61. // Extensions
  62. DrawExtensionsWidgetInInspector();
  63. }
  64. private string[] mCameraCandidates;
  65. private Dictionary<CinemachineVirtualCameraBase, int> mCameraIndexLookup;
  66. private void UpdateCameraCandidates()
  67. {
  68. List<string> vcams = new List<string>();
  69. mCameraIndexLookup = new Dictionary<CinemachineVirtualCameraBase, int>();
  70. vcams.Add("(none)");
  71. CinemachineVirtualCameraBase[] children = Target.ChildCameras;
  72. foreach (var c in children)
  73. {
  74. mCameraIndexLookup[c] = vcams.Count;
  75. vcams.Add(c.Name);
  76. }
  77. mCameraCandidates = vcams.ToArray();
  78. }
  79. private int GetCameraIndex(Object obj)
  80. {
  81. if (obj == null || mCameraIndexLookup == null)
  82. return 0;
  83. CinemachineVirtualCameraBase vcam = obj as CinemachineVirtualCameraBase;
  84. if (vcam == null)
  85. return 0;
  86. if (!mCameraIndexLookup.ContainsKey(vcam))
  87. return 0;
  88. return mCameraIndexLookup[vcam];
  89. }
  90. void SetupInstructionList()
  91. {
  92. mInstructionList = new UnityEditorInternal.ReorderableList(serializedObject,
  93. serializedObject.FindProperty(() => Target.m_Instructions),
  94. true, true, true, true);
  95. // Needed for accessing field names as strings
  96. CinemachineBlendListCamera.Instruction def = new CinemachineBlendListCamera.Instruction();
  97. float vSpace = 2;
  98. float hSpace = 3;
  99. float floatFieldWidth = EditorGUIUtility.singleLineHeight * 2.5f;
  100. float hBigSpace = EditorGUIUtility.singleLineHeight * 2 / 3;
  101. mInstructionList.drawHeaderCallback = (Rect rect) =>
  102. {
  103. float sharedWidth = rect.width - EditorGUIUtility.singleLineHeight
  104. - floatFieldWidth - hSpace - hBigSpace;
  105. rect.x += EditorGUIUtility.singleLineHeight; rect.width = sharedWidth / 2;
  106. EditorGUI.LabelField(rect, "Child");
  107. rect.x += rect.width + hSpace;
  108. EditorGUI.LabelField(rect, "Blend in");
  109. rect.x += rect.width + hBigSpace; rect.width = floatFieldWidth;
  110. EditorGUI.LabelField(rect, "Hold");
  111. };
  112. mInstructionList.drawElementCallback
  113. = (Rect rect, int index, bool isActive, bool isFocused) =>
  114. {
  115. SerializedProperty instProp = mInstructionList.serializedProperty.GetArrayElementAtIndex(index);
  116. float sharedWidth = rect.width - floatFieldWidth - hSpace - hBigSpace;
  117. rect.y += vSpace; rect.height = EditorGUIUtility.singleLineHeight;
  118. rect.width = sharedWidth / 2;
  119. SerializedProperty vcamSelProp = instProp.FindPropertyRelative(() => def.m_VirtualCamera);
  120. int currentVcam = GetCameraIndex(vcamSelProp.objectReferenceValue);
  121. int vcamSelection = EditorGUI.Popup(rect, currentVcam, mCameraCandidates);
  122. if (currentVcam != vcamSelection)
  123. vcamSelProp.objectReferenceValue = (vcamSelection == 0)
  124. ? null : Target.ChildCameras[vcamSelection - 1];
  125. rect.x += rect.width + hSpace; rect.width = sharedWidth / 2;
  126. if (index > 0 || Target.m_Loop)
  127. EditorGUI.PropertyField(rect, instProp.FindPropertyRelative(() => def.m_Blend),
  128. GUIContent.none);
  129. if (index < mInstructionList.count - 1 || Target.m_Loop)
  130. {
  131. float oldWidth = EditorGUIUtility.labelWidth;
  132. EditorGUIUtility.labelWidth = hBigSpace;
  133. rect.x += rect.width; rect.width = floatFieldWidth + hBigSpace;
  134. SerializedProperty holdProp = instProp.FindPropertyRelative(() => def.m_Hold);
  135. EditorGUI.PropertyField(rect, holdProp, new GUIContent(" ", holdProp.tooltip));
  136. holdProp.floatValue = Mathf.Max(holdProp.floatValue, 0);
  137. EditorGUIUtility.labelWidth = oldWidth;
  138. }
  139. };
  140. }
  141. void SetupChildList()
  142. {
  143. float vSpace = 2;
  144. mChildList = new UnityEditorInternal.ReorderableList(serializedObject,
  145. serializedObject.FindProperty(() => Target.m_ChildCameras),
  146. true, true, true, true);
  147. mChildList.drawHeaderCallback = (Rect rect) =>
  148. {
  149. EditorGUI.LabelField(rect, "Virtual Camera Children");
  150. };
  151. mChildList.drawElementCallback
  152. = (Rect rect, int index, bool isActive, bool isFocused) =>
  153. {
  154. rect.y += vSpace;
  155. Vector2 pos = rect.position;
  156. rect.height = EditorGUIUtility.singleLineHeight;
  157. SerializedProperty element
  158. = mChildList.serializedProperty.GetArrayElementAtIndex(index);
  159. EditorGUI.PropertyField(rect, element, GUIContent.none);
  160. };
  161. mChildList.onChangedCallback = (UnityEditorInternal.ReorderableList l) =>
  162. {
  163. if (l.index < 0 || l.index >= l.serializedProperty.arraySize)
  164. return;
  165. Object o = l.serializedProperty.GetArrayElementAtIndex(
  166. l.index).objectReferenceValue;
  167. CinemachineVirtualCameraBase vcam = (o != null)
  168. ? (o as CinemachineVirtualCameraBase) : null;
  169. if (vcam != null)
  170. vcam.transform.SetSiblingIndex(l.index);
  171. };
  172. mChildList.onAddCallback = (UnityEditorInternal.ReorderableList l) =>
  173. {
  174. var index = l.serializedProperty.arraySize;
  175. var vcam = CinemachineMenu.CreateDefaultVirtualCamera();
  176. Undo.SetTransformParent(vcam.transform, Target.transform, "");
  177. vcam.transform.SetSiblingIndex(index);
  178. };
  179. mChildList.onRemoveCallback = (UnityEditorInternal.ReorderableList l) =>
  180. {
  181. Object o = l.serializedProperty.GetArrayElementAtIndex(
  182. l.index).objectReferenceValue;
  183. CinemachineVirtualCameraBase vcam = (o != null)
  184. ? (o as CinemachineVirtualCameraBase) : null;
  185. if (vcam != null)
  186. Undo.DestroyObjectImmediate(vcam.gameObject);
  187. };
  188. }
  189. }
  190. }