TargetedDrawer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using Chronos.Controls.Editor;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityObject = UnityEngine.Object;
  6. namespace Chronos.Reflection.Editor
  7. {
  8. public abstract class TargetedDrawer : PropertyDrawer
  9. {
  10. /// <summary>
  11. /// Whether the self-targeted attribute is defined on the inspected field.
  12. /// </summary>
  13. protected bool isSelfTargeted;
  14. protected bool showTargetField
  15. {
  16. get
  17. {
  18. return !isSelfTargeted || ShowSelfTargetField;
  19. }
  20. }
  21. /// <summary>
  22. /// The UnityMember.target of the inspected property, of type Object.
  23. /// </summary>
  24. protected SerializedProperty targetProperty;
  25. #region Graphical Configuration
  26. /// <summary>
  27. /// Whether the target field should be shown in self-targetting mode.
  28. /// </summary>
  29. protected const bool ShowSelfTargetField = false;
  30. /// <summary>
  31. /// The padding between the label and the target and member controls, in vertical display.
  32. /// </summary>
  33. protected const float LabelPadding = 2;
  34. /// <summary>
  35. /// The padding between the target and member controls, in vertical display.
  36. /// </summary>
  37. protected const float InnerPadding = 5;
  38. /// <summary>
  39. /// The padding below the drawer, in vertical display.
  40. /// </summary>
  41. protected const float BottomPadding = 5;
  42. #endregion
  43. /// <summary>
  44. /// Initializes the members of the drawer via the specified property.
  45. /// </summary>
  46. protected virtual void Update(SerializedProperty property)
  47. {
  48. this.targetProperty = property.FindPropertyRelative("_target");
  49. isSelfTargeted = Attribute.IsDefined(fieldInfo, typeof(SelfTargetedAttribute));
  50. }
  51. /// <summary>
  52. /// Calculates the height of the drawer.
  53. /// </summary>
  54. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  55. {
  56. Update(property);
  57. // Double the height and add the padding for self-targeting,
  58. // because we'll display the controls on another line.
  59. if (showTargetField && !string.IsNullOrEmpty(label.text))
  60. {
  61. return base.GetPropertyHeight(property, label) * 2 + LabelPadding + BottomPadding;
  62. }
  63. else
  64. {
  65. return base.GetPropertyHeight(property, label);
  66. }
  67. }
  68. /// <summary>
  69. /// Renders the drawer.
  70. /// </summary>
  71. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  72. {
  73. Update(property);
  74. EditorGUI.BeginProperty(position, label, property);
  75. // Hack the indent level for full control
  76. position = EditorGUI.IndentedRect(position);
  77. int oldIndent = EditorGUI.indentLevel;
  78. EditorGUI.indentLevel = 0;
  79. // Positioning
  80. // When in self targeting mode, hide the target field and display
  81. // the member popup as a normal field. When in manual targeting, push
  82. // the field down below the label.
  83. Rect targetPosition;
  84. Rect memberPosition;
  85. if (showTargetField)
  86. {
  87. if (!string.IsNullOrEmpty(label.text))
  88. {
  89. position.height = base.GetPropertyHeight(property, label);
  90. position.y += EditorGUI.PrefixLabel(position, label).height + LabelPadding;
  91. }
  92. targetPosition = position;
  93. memberPosition = position;
  94. targetPosition.width *= (1f / 3f);
  95. targetPosition.width -= (InnerPadding / 2);
  96. memberPosition.width *= (2f / 3f);
  97. memberPosition.width -= (InnerPadding / 2);
  98. memberPosition.x = targetPosition.xMax + InnerPadding;
  99. }
  100. else
  101. {
  102. targetPosition = new Rect(0, 0, 0, 0);
  103. memberPosition = EditorGUI.PrefixLabel(position, label);
  104. }
  105. // Render controls
  106. RenderTargetControl(targetPosition);
  107. RenderMemberControl(memberPosition);
  108. // Restore the indent level
  109. EditorGUI.indentLevel = oldIndent;
  110. EditorGUI.EndProperty();
  111. }
  112. protected virtual void RenderTargetControl(Rect position)
  113. {
  114. // When in self targeting mode, assign the target property to the
  115. // target object behind the scenes. When in manual targeting mode,
  116. // display a standard Object property field.
  117. if (isSelfTargeted)
  118. {
  119. foreach (var singleTargetProperty in targetProperty.Multiple())
  120. {
  121. singleTargetProperty.objectReferenceValue = GetSelfTarget(singleTargetProperty.serializedObject.targetObject);
  122. }
  123. }
  124. if (showTargetField)
  125. {
  126. EditorGUI.BeginDisabledGroup(isSelfTargeted);
  127. EditorGUI.PropertyField(position, targetProperty, GUIContent.none);
  128. EditorGUI.EndDisabledGroup();
  129. }
  130. }
  131. protected abstract void RenderMemberControl(Rect position);
  132. /// <summary>
  133. /// Returns the object assigned as self-target from a serialized object.
  134. /// </summary>
  135. protected virtual UnityObject GetSelfTarget(UnityObject obj)
  136. {
  137. return obj;
  138. }
  139. }
  140. }