Spacing using Foldout with PropertyDrawer

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

using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class VariableReferenceEditor : PropertyDrawer
{
    SerializedProperty useConstant;
    SerializedProperty constantValue;
    SerializedProperty variable;

    public virtual UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
    {
        return null;
    }

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        //base.OnGUI(position, property, label);
        EditorGUI.BeginProperty(position, label, property);

        Rect fieldRect = new Rect(position);
        var indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;

        property.isExpanded = EditorGUI.Foldout(fieldRect, property.isExpanded, new GUIContent(property.name), true);
        if(!property.isExpanded)
        {
            return;
        }

        ++indent;

        Rect constanctRect = new Rect(position.x, position.y + EditorGUIUtility.singleLineHeight, position.width, EditorGUIUtility.singleLineHeight);
        Rect valueRect = new Rect(position.x, position.y + (EditorGUIUtility.singleLineHeight * 2), position.width, EditorGUIUtility.singleLineHeight);

        useConstant = property.FindPropertyRelative("UseConstant");
        constantValue = property.FindPropertyRelative("ConstantValue");
        variable = property.FindPropertyRelative("Variable");

        useConstant.boolValue = EditorGUI.Toggle(constanctRect, "Use constant", useConstant.boolValue);
        if(useConstant.boolValue)
        {
            EditorGUI.PropertyField(valueRect, constantValue, new GUIContent("Constant Value"));
        }
        else
        {
            variable.objectReferenceValue = GetObjectField(valueRect, variable);
        }

        property.serializedObject.ApplyModifiedProperties();
   
        EditorGUI.indentLevel = indent;
        EditorGUI.EndProperty();
    }

    //This will need to be adjusted based on what you are displaying
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        return base.GetPropertyHeight(property, label);
    }
}

[CustomPropertyDrawer(typeof(VariableReferenceBool))]
public class VariableReferenceBoolEditor : VariableReferenceEditor
{
public override UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
    {
       return EditorGUI.ObjectField(inRect, "Variable", inProperty.objectReferenceValue, typeof(VariableBool), false);
    }
}

[CustomPropertyDrawer(typeof(VariableReferenceFloat))]
public class VariableReferenceFloatEditor : VariableReferenceEditor
{
    public override UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
    {
        return EditorGUI.ObjectField(inRect, "Variable", inProperty.objectReferenceValue, typeof(VariableFloat), false);
    }
}

[CustomPropertyDrawer(typeof(VariableReferenceString))]
public class VariableReferenceStringEditor : VariableReferenceEditor
{
    public override UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
    {
        return EditorGUI.ObjectField(inRect, "Variable", inProperty.objectReferenceValue, typeof(VariableString), false);
    }
}

[CustomPropertyDrawer(typeof(VariableReferenceInt))]
public class VariableReferenceIntEditor : VariableReferenceEditor
{
    public override UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
    {
        return EditorGUI.ObjectField(inRect, "Variable", inProperty.objectReferenceValue, typeof(VariableInt), false);
    }
}

and that’s the result i got

Thanks

Hi @fouratj ,

From what I saw in your code, the GetPropertyHeight is returning the same value when you have your field fold and unfold.

Please check this Unity Answers page: How can PropertyDrawer with fouldouts to draw at the appropriate height? - Questions & Answers - Unity Discussions

You’ll find a similar situation that you have and some code samples that solve the same problem that you’re currently having.

Good luck with it!