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