CinemachineMixingCameraEditor.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using UnityEditor;
  2. using UnityEngine;
  3. using Cinemachine.Utility;
  4. using System.Collections.Generic;
  5. namespace Cinemachine.Editor
  6. {
  7. [CustomEditor(typeof(CinemachineMixingCamera))]
  8. internal sealed class CinemachineMixingCameraEditor
  9. : CinemachineVirtualCameraBaseEditor<CinemachineMixingCamera>
  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. for (int i = 0; i < CinemachineMixingCamera.MaxCameras; ++i)
  17. excluded.Add(WeightPropertyName(i));
  18. }
  19. static string WeightPropertyName(int i) { return "m_Weight" + i; }
  20. public override void OnInspectorGUI()
  21. {
  22. BeginInspector();
  23. DrawHeaderInInspector();
  24. DrawRemainingPropertiesInInspector();
  25. float totalWeight = 0;
  26. CinemachineVirtualCameraBase[] children = Target.ChildCameras;
  27. int numCameras = Mathf.Min(CinemachineMixingCamera.MaxCameras, children.Length);
  28. for (int i = 0; i < numCameras; ++i)
  29. if (children[i].isActiveAndEnabled)
  30. totalWeight += Target.GetWeight(i);
  31. if (numCameras == 0)
  32. EditorGUILayout.HelpBox("There are no Virtual Camera children", MessageType.Warning);
  33. else
  34. {
  35. EditorGUILayout.Separator();
  36. EditorGUILayout.LabelField("Child Camera Weights", EditorStyles.boldLabel);
  37. for (int i = 0; i < numCameras; ++i)
  38. {
  39. SerializedProperty prop = serializedObject.FindProperty(WeightPropertyName(i));
  40. if (prop != null)
  41. EditorGUILayout.PropertyField(prop, new GUIContent(children[i].Name));
  42. }
  43. serializedObject.ApplyModifiedProperties();
  44. if (totalWeight <= UnityVectorExtensions.Epsilon)
  45. EditorGUILayout.HelpBox("No input channels are active", MessageType.Warning);
  46. if (children.Length > numCameras)
  47. EditorGUILayout.HelpBox(
  48. "There are " + children.Length
  49. + " child cameras. A maximum of " + numCameras + " is supported.",
  50. MessageType.Warning);
  51. // Camera proportion indicator
  52. EditorGUILayout.Separator();
  53. EditorGUILayout.LabelField("Mix Result", EditorStyles.boldLabel);
  54. DrawProportionIndicator(children, numCameras, totalWeight);
  55. }
  56. // Extensions
  57. DrawExtensionsWidgetInInspector();
  58. }
  59. void DrawProportionIndicator(
  60. CinemachineVirtualCameraBase[] children, int numCameras, float totalWeight)
  61. {
  62. GUIStyle style = EditorStyles.centeredGreyMiniLabel;
  63. Color bkg = new Color(0.27f, 0.27f, 0.27f); // ack! no better way than this?
  64. Color fg = Color.Lerp(CinemachineBrain.GetSoloGUIColor(), bkg, 0.8f);
  65. float totalHeight = (style.lineHeight + style.margin.vertical) * numCameras;
  66. Rect r = EditorGUILayout.GetControlRect(true, totalHeight);
  67. r.height /= numCameras; r.height -= 1;
  68. float fullWidth = r.width;
  69. for (int i = 0; i < numCameras; ++i)
  70. {
  71. float p = 0;
  72. string label = children[i].Name;
  73. if (totalWeight > UnityVectorExtensions.Epsilon)
  74. {
  75. if (children[i].isActiveAndEnabled)
  76. p = Target.GetWeight(i) / totalWeight;
  77. else
  78. label += " (disabled)";
  79. }
  80. r.width = fullWidth * p;
  81. EditorGUI.DrawRect(r, fg);
  82. Rect r2 = r;
  83. r2.x += r.width;
  84. r2.width = fullWidth - r.width;
  85. EditorGUI.DrawRect(r2, bkg);
  86. r.width = fullWidth;
  87. EditorGUI.LabelField(r, label, style);
  88. r.y += r.height + 1;
  89. }
  90. }
  91. }
  92. }