Search Unity

Question Spacing using Foldout with PropertyDrawer

Discussion in 'Editor & General Support' started by fouratj, Jul 21, 2020.

  1. fouratj

    fouratj

    Joined:
    Sep 20, 2019
    Posts:
    9
    HI there,

    I'm facing an issue related to spacing element when i use the Foldout property.
    When i fold my element, the top elements override the one below them, and i'm wondering how to fix that, how the keep the spacing relative, should i use some kind of box gui or something like that ?

    Here's the script i've made

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    5. public class VariableReferenceEditor : PropertyDrawer
    6. {
    7.     SerializedProperty useConstant;
    8.     SerializedProperty constantValue;
    9.     SerializedProperty variable;
    10.  
    11.     public virtual UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
    12.     {
    13.         return null;
    14.     }
    15.  
    16.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    17.     {
    18.         //base.OnGUI(position, property, label);
    19.         EditorGUI.BeginProperty(position, label, property);
    20.  
    21.         Rect fieldRect = new Rect(position);
    22.         var indent = EditorGUI.indentLevel;
    23.         EditorGUI.indentLevel = 0;
    24.  
    25.         property.isExpanded = EditorGUI.Foldout(fieldRect, property.isExpanded, new GUIContent(property.name), true);
    26.         if(!property.isExpanded)
    27.         {
    28.             return;
    29.         }
    30.  
    31.         ++indent;
    32.  
    33.         Rect constanctRect = new Rect(position.x, position.y + EditorGUIUtility.singleLineHeight, position.width, EditorGUIUtility.singleLineHeight);
    34.         Rect valueRect = new Rect(position.x, position.y + (EditorGUIUtility.singleLineHeight * 2), position.width, EditorGUIUtility.singleLineHeight);
    35.  
    36.         useConstant = property.FindPropertyRelative("UseConstant");
    37.         constantValue = property.FindPropertyRelative("ConstantValue");
    38.         variable = property.FindPropertyRelative("Variable");
    39.  
    40.         useConstant.boolValue = EditorGUI.Toggle(constanctRect, "Use constant", useConstant.boolValue);
    41.         if(useConstant.boolValue)
    42.         {
    43.             EditorGUI.PropertyField(valueRect, constantValue, new GUIContent("Constant Value"));
    44.         }
    45.         else
    46.         {
    47.             variable.objectReferenceValue = GetObjectField(valueRect, variable);
    48.         }
    49.  
    50.         property.serializedObject.ApplyModifiedProperties();
    51.    
    52.         EditorGUI.indentLevel = indent;
    53.         EditorGUI.EndProperty();
    54.     }
    55.  
    56.     //This will need to be adjusted based on what you are displaying
    57.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    58.     {
    59.         return base.GetPropertyHeight(property, label);
    60.     }
    61. }
    62.  
    63. [CustomPropertyDrawer(typeof(VariableReferenceBool))]
    64. public class VariableReferenceBoolEditor : VariableReferenceEditor
    65. {
    66. public override UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
    67.     {
    68.        return EditorGUI.ObjectField(inRect, "Variable", inProperty.objectReferenceValue, typeof(VariableBool), false);
    69.     }
    70. }
    71.  
    72. [CustomPropertyDrawer(typeof(VariableReferenceFloat))]
    73. public class VariableReferenceFloatEditor : VariableReferenceEditor
    74. {
    75.     public override UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
    76.     {
    77.         return EditorGUI.ObjectField(inRect, "Variable", inProperty.objectReferenceValue, typeof(VariableFloat), false);
    78.     }
    79. }
    80.  
    81. [CustomPropertyDrawer(typeof(VariableReferenceString))]
    82. public class VariableReferenceStringEditor : VariableReferenceEditor
    83. {
    84.     public override UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
    85.     {
    86.         return EditorGUI.ObjectField(inRect, "Variable", inProperty.objectReferenceValue, typeof(VariableString), false);
    87.     }
    88. }
    89.  
    90. [CustomPropertyDrawer(typeof(VariableReferenceInt))]
    91. public class VariableReferenceIntEditor : VariableReferenceEditor
    92. {
    93.     public override UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
    94.     {
    95.         return EditorGUI.ObjectField(inRect, "Variable", inProperty.objectReferenceValue, typeof(VariableInt), false);
    96.     }
    97. }
    and that's the result i got

    upload_2020-7-21_12-7-8.png


    Thanks
     
    Last edited: Jul 21, 2020
  2. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    507