CinemachineBlenderSettingsEditor.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditorInternal;
  4. using System.Collections.Generic;
  5. namespace Cinemachine.Editor
  6. {
  7. [CustomEditor(typeof(CinemachineBlenderSettings))]
  8. internal sealed class CinemachineBlenderSettingsEditor : BaseEditor<CinemachineBlenderSettings>
  9. {
  10. private ReorderableList mBlendList;
  11. /// <summary>
  12. /// Called when building the Camera popup menus, to get the domain of possible
  13. /// cameras. If no delegate is set, will find all top-level (non-slave)
  14. /// virtual cameras in the scene.
  15. /// </summary>
  16. public GetAllVirtualCamerasDelegate GetAllVirtualCameras;
  17. public delegate CinemachineVirtualCameraBase[] GetAllVirtualCamerasDelegate();
  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_CustomBlends));
  24. }
  25. public override void OnInspectorGUI()
  26. {
  27. BeginInspector();
  28. if (mBlendList == null)
  29. SetupBlendList();
  30. DrawRemainingPropertiesInInspector();
  31. UpdateCameraCandidates();
  32. mBlendList.DoLayoutList();
  33. serializedObject.ApplyModifiedProperties();
  34. }
  35. private const string kNoneLabel = "(none)";
  36. private string[] mCameraCandidates;
  37. private Dictionary<string, int> mCameraIndexLookup;
  38. private void UpdateCameraCandidates()
  39. {
  40. List<string> vcams = new List<string>();
  41. mCameraIndexLookup = new Dictionary<string, int>();
  42. CinemachineVirtualCameraBase[] candidates;
  43. if (GetAllVirtualCameras != null)
  44. candidates = GetAllVirtualCameras();
  45. else
  46. {
  47. // Get all top-level (i.e. non-slave) virtual cameras
  48. candidates = Resources.FindObjectsOfTypeAll(
  49. typeof(CinemachineVirtualCameraBase)) as CinemachineVirtualCameraBase[];
  50. for (int i = 0; i < candidates.Length; ++i)
  51. if (candidates[i].ParentCamera != null)
  52. candidates[i] = null;
  53. }
  54. vcams.Add(kNoneLabel);
  55. vcams.Add(CinemachineBlenderSettings.kBlendFromAnyCameraLabel);
  56. foreach (CinemachineVirtualCameraBase c in candidates)
  57. if (c != null && !vcams.Contains(c.Name))
  58. vcams.Add(c.Name);
  59. mCameraCandidates = vcams.ToArray();
  60. for (int i = 0; i < mCameraCandidates.Length; ++i)
  61. mCameraIndexLookup[mCameraCandidates[i]] = i;
  62. }
  63. private int GetCameraIndex(string name)
  64. {
  65. if (name == null || mCameraIndexLookup == null)
  66. return 0;
  67. if (!mCameraIndexLookup.ContainsKey(name))
  68. return 0;
  69. return mCameraIndexLookup[name];
  70. }
  71. void DrawVcamSelector(Rect r, SerializedProperty prop)
  72. {
  73. r.width -= EditorGUIUtility.singleLineHeight;
  74. int current = GetCameraIndex(prop.stringValue);
  75. var oldColor = GUI.color;
  76. if (current == 0)
  77. GUI.color = new Color(1, 193.0f/255.0f, 7.0f/255.0f); // the "warning" icon color
  78. EditorGUI.PropertyField(r, prop, GUIContent.none);
  79. r.x += r.width; r.width = EditorGUIUtility.singleLineHeight;
  80. int sel = EditorGUI.Popup(r, current, mCameraCandidates);
  81. if (current != sel)
  82. prop.stringValue = (mCameraCandidates[sel] == kNoneLabel)
  83. ? string.Empty : mCameraCandidates[sel];
  84. GUI.color = oldColor;
  85. }
  86. void SetupBlendList()
  87. {
  88. mBlendList = new ReorderableList(serializedObject,
  89. serializedObject.FindProperty(() => Target.m_CustomBlends),
  90. true, true, true, true);
  91. // Needed for accessing string names of fields
  92. CinemachineBlenderSettings.CustomBlend def = new CinemachineBlenderSettings.CustomBlend();
  93. CinemachineBlendDefinition def2 = new CinemachineBlendDefinition();
  94. float vSpace = 2;
  95. float hSpace = 3;
  96. float floatFieldWidth = EditorGUIUtility.singleLineHeight * 2.5f;
  97. mBlendList.drawHeaderCallback = (Rect rect) =>
  98. {
  99. rect.width -= (EditorGUIUtility.singleLineHeight + 2 * hSpace);
  100. rect.width /= 3;
  101. rect.x += EditorGUIUtility.singleLineHeight;
  102. EditorGUI.LabelField(rect, "From");
  103. rect.x += rect.width + hSpace;
  104. EditorGUI.LabelField(rect, "To");
  105. rect.x += rect.width + hSpace; rect.width -= floatFieldWidth + hSpace;
  106. EditorGUI.LabelField(rect, "Style");
  107. rect.x += rect.width + hSpace; rect.width = floatFieldWidth;
  108. EditorGUI.LabelField(rect, "Time");
  109. };
  110. mBlendList.drawElementCallback
  111. = (Rect rect, int index, bool isActive, bool isFocused) =>
  112. {
  113. SerializedProperty element
  114. = mBlendList.serializedProperty.GetArrayElementAtIndex(index);
  115. rect.y += vSpace;
  116. rect.height = EditorGUIUtility.singleLineHeight;
  117. rect.width -= 2 * hSpace; rect.width /= 3;
  118. DrawVcamSelector(rect, element.FindPropertyRelative(() => def.m_From));
  119. rect.x += rect.width + hSpace;
  120. DrawVcamSelector(rect, element.FindPropertyRelative(() => def.m_To));
  121. SerializedProperty blendProp = element.FindPropertyRelative(() => def.m_Blend);
  122. rect.x += rect.width + hSpace;
  123. EditorGUI.PropertyField(rect, blendProp, GUIContent.none);
  124. };
  125. mBlendList.onAddCallback = (ReorderableList l) =>
  126. {
  127. var index = l.serializedProperty.arraySize;
  128. ++l.serializedProperty.arraySize;
  129. SerializedProperty blendProp = l.serializedProperty.GetArrayElementAtIndex(
  130. index).FindPropertyRelative(() => def.m_Blend);
  131. blendProp.FindPropertyRelative(() => def2.m_Style).enumValueIndex
  132. = (int)CinemachineBlendDefinition.Style.EaseInOut;
  133. blendProp.FindPropertyRelative(() => def2.m_Time).floatValue = 2f;
  134. };
  135. }
  136. }
  137. }