Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RelativePropertiesDrawer.cs 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using UnityEditor.UIElements;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. namespace UnityEditor.Rendering
  5. {
  6. /// <summary>
  7. /// Custom property drawer that renders automatically a set of properties of a given object
  8. /// </summary>
  9. public abstract class RelativePropertiesDrawer : PropertyDrawer
  10. {
  11. /// <summary>
  12. /// The set of properties to draw the default <see cref="PropertyDrawer"/>.
  13. /// </summary>
  14. protected abstract string[] relativePropertiesNames { get; }
  15. /// <inheritdoc/>
  16. public override VisualElement CreatePropertyGUI(SerializedProperty property)
  17. {
  18. var container = new VisualElement();
  19. for (int i = 0; i < relativePropertiesNames.Length; ++i)
  20. {
  21. var relativeProperty = property.FindPropertyRelative(relativePropertiesNames[i]);
  22. if (relativeProperty == null)
  23. continue;
  24. container.Add(new PropertyField(relativeProperty));
  25. }
  26. return container;
  27. }
  28. /// <inheritdoc/>
  29. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  30. {
  31. EditorGUI.BeginProperty(position, label, property);
  32. Rect? rect = null;
  33. for (int i = 0; i < relativePropertiesNames.Length; ++i)
  34. {
  35. var relativeProperty = property.FindPropertyRelative(relativePropertiesNames[i]);
  36. if (relativeProperty == null)
  37. continue;
  38. var height = EditorGUI.GetPropertyHeight(relativeProperty, true) +
  39. EditorGUIUtility.standardVerticalSpacing;
  40. rect = rect != null ?
  41. new Rect(rect.Value.x, rect.Value.y + rect.Value.height + EditorGUIUtility.standardVerticalSpacing, rect.Value.width, height) :
  42. new Rect(position.x, position.y + EditorGUIUtility.standardVerticalSpacing, position.width, height);
  43. EditorGUI.PropertyField(rect.Value, relativeProperty);
  44. }
  45. EditorGUI.EndProperty();
  46. }
  47. /// <inheritdoc/>
  48. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  49. {
  50. float height = EditorGUIUtility.standardVerticalSpacing;
  51. for (int i = 0; i < relativePropertiesNames.Length; ++i)
  52. {
  53. var relativeProperty = property.FindPropertyRelative(relativePropertiesNames[i]);
  54. if (relativeProperty == null)
  55. continue;
  56. height += EditorGUI.GetPropertyHeight(relativeProperty, true) + EditorGUIUtility.standardVerticalSpacing;
  57. }
  58. return height;
  59. }
  60. }
  61. }