CinemachineTargetGroupEditor.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. namespace Cinemachine.Editor
  6. {
  7. [CustomEditor(typeof(CinemachineTargetGroup))]
  8. internal sealed class CinemachineTargetGroupEditor : BaseEditor<CinemachineTargetGroup>
  9. {
  10. private UnityEditorInternal.ReorderableList mTargetList;
  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_Targets));
  17. }
  18. void OnEnable()
  19. {
  20. mTargetList = null;
  21. }
  22. public override void OnInspectorGUI()
  23. {
  24. BeginInspector();
  25. DrawRemainingPropertiesInInspector();
  26. if (mTargetList == null)
  27. SetupTargetList();
  28. EditorGUI.BeginChangeCheck();
  29. mTargetList.DoLayoutList();
  30. DisplayErrorMessageForDescendants();
  31. if (EditorGUI.EndChangeCheck())
  32. serializedObject.ApplyModifiedProperties();
  33. }
  34. void DisplayErrorMessageForDescendants()
  35. {
  36. String indices = "";
  37. for (int i = 0; i < Target.m_Targets.Length; ++i)
  38. {
  39. if (Target.m_Targets[i].target != null && Target.m_Targets[i].target.IsChildOf(Target.Transform))
  40. {
  41. indices += i + ", ";
  42. }
  43. }
  44. if (indices.Length > 0)
  45. {
  46. indices = indices.Substring(0, indices.Length - 2);
  47. EditorGUILayout.HelpBox(
  48. "Group members at index {" + indices + "} are child gameobjects of the group. " +
  49. "This is not supported and may cause undefined behaviour. Unparent them from the group.",
  50. MessageType.Error);
  51. }
  52. }
  53. void SetupTargetList()
  54. {
  55. float vSpace = 2;
  56. float floatFieldWidth = EditorGUIUtility.singleLineHeight * 3.5f;
  57. float hBigSpace = EditorGUIUtility.singleLineHeight * 2 / 3;
  58. mTargetList = new UnityEditorInternal.ReorderableList(
  59. serializedObject, FindProperty(x => x.m_Targets),
  60. true, true, true, true);
  61. // Needed for accessing field names as strings
  62. CinemachineTargetGroup.Target def = new CinemachineTargetGroup.Target();
  63. mTargetList.drawHeaderCallback = (Rect rect) =>
  64. {
  65. rect.width -= EditorGUIUtility.singleLineHeight + 2 * (floatFieldWidth + hBigSpace);
  66. Vector2 pos = rect.position; pos.x += EditorGUIUtility.singleLineHeight;
  67. rect.position = pos;
  68. EditorGUI.LabelField(rect, "Target");
  69. pos.x += rect.width + hBigSpace; rect.width = floatFieldWidth + hBigSpace; rect.position = pos;
  70. EditorGUI.LabelField(rect, "Weight");
  71. pos.x += rect.width; rect.position = pos;
  72. EditorGUI.LabelField(rect, "Radius");
  73. };
  74. mTargetList.drawElementCallback
  75. = (Rect rect, int index, bool isActive, bool isFocused) =>
  76. {
  77. SerializedProperty elemProp = mTargetList.serializedProperty.GetArrayElementAtIndex(index);
  78. rect.y += vSpace;
  79. rect.height = EditorGUIUtility.singleLineHeight;
  80. Vector2 pos = rect.position;
  81. //rect.width -= hSpace + 2 * EditorGUIUtility.singleLineHeight;
  82. rect.width -= 2 * (floatFieldWidth + hBigSpace);
  83. EditorGUI.PropertyField(rect, elemProp.FindPropertyRelative(() => def.target), GUIContent.none);
  84. float oldWidth = EditorGUIUtility.labelWidth;
  85. EditorGUIUtility.labelWidth = EditorGUIUtility.singleLineHeight;
  86. pos.x += rect.width; rect.width = floatFieldWidth + hBigSpace; rect.position = pos;
  87. EditorGUI.PropertyField(rect, elemProp.FindPropertyRelative(() => def.weight), new GUIContent(" "));
  88. pos.x += rect.width; rect.position = pos;
  89. EditorGUI.PropertyField(rect, elemProp.FindPropertyRelative(() => def.radius), new GUIContent(" "));
  90. EditorGUIUtility.labelWidth = oldWidth;
  91. };
  92. mTargetList.onAddCallback = (UnityEditorInternal.ReorderableList l) =>
  93. {
  94. var index = l.serializedProperty.arraySize;
  95. ++l.serializedProperty.arraySize;
  96. SerializedProperty elemProp = mTargetList.serializedProperty.GetArrayElementAtIndex(index);
  97. elemProp.FindPropertyRelative(() => def.weight).floatValue = 1;
  98. };
  99. }
  100. #if false // enable for debugging
  101. [DrawGizmo(GizmoType.Active | GizmoType.InSelectionHierarchy, typeof(CinemachineTargetGroup))]
  102. private static void DrawGroupComposerGizmos(CinemachineTargetGroup target, GizmoType selectionType)
  103. {
  104. Gizmos.color = Color.yellow;
  105. var sphere = target.Sphere;
  106. Gizmos.DrawWireSphere(sphere.position, sphere.radius);
  107. }
  108. #endif
  109. }
  110. }