EditorGUI drawing on TOP of my inspector window [Issue]

Hi I am using EditorGUI as such →

using UnityEngine;
using UnityEditor;

[CustomPropertyDrawer(typeof(Field))]
public class CustomFieldDrawer : PropertyDrawer {

    SerializedProperty name;
    SerializedProperty prefab;
    SerializedProperty usePrefab;
    SerializedProperty fieldMesh;
    SerializedProperty fieldMaterial;
    SerializedProperty scatter;

    private int indent;

    public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
    {

        name       = prop.FindPropertyRelative("FieldName");
        prefab    = prop.FindPropertyRelative("Prefab");
        usePrefab = prop.FindPropertyRelative("usePrefab");
        fieldMesh = prop.FindPropertyRelative("FieldMesh");
        fieldMaterial = prop.FindPropertyRelative("FieldMaterial");
        scatter       = prop.FindPropertyRelative("Scatter");

        indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;


        EditorGUI.PropertyField(pos, prop ,new GUIContent(name.stringValue==""? label.text : name.stringValue));

        if(prop.isExpanded)
        {

            EditorGUI.PropertyField(new Rect(pos.x,pos.y+pos.height,pos.width,pos.height),name );

   
            EditorGUI.PropertyField(new Rect(pos.x,pos.y+pos.height*2,pos.width,pos.height),usePrefab, new GUIContent("Use a Prefab?"));
            if(usePrefab.boolValue)
            {
                EditorGUI.PropertyField(new Rect(pos.x,pos.y+pos.height*3,pos.width,pos.height*2), prefab,new GUIContent("prefab"));
            }
            else
            {
                EditorGUI.PropertyField(new Rect(pos.x,pos.y+pos.height*4,pos.width,pos.height*2), fieldMesh);
                EditorGUI.PropertyField(new Rect(pos.x,pos.y+pos.height*5,pos.width,pos.height),fieldMaterial);

            }

            EditorGUI.PropertyField(new Rect(pos.x,pos.y+pos.height*6,pos.width,pos.height), scatter,new GUIContent("Scattering Properties"),true);
   


        }

        EditorGUI.indentLevel = indent;

    }


}

But it draws it on TOP of the inspector (Picture Below)-

what am I doing wrong? (I should mention that “Field Builder” is a script that initializes an array of serializable type “Field” (the one with my custom PropertyDrawer)

any help is greatly appreciated :slight_smile:

Override PropertyDrawer.GetPropertyHeight(). Something like:

public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
    var numLines = usePrefab.boolValue ? 5 : 4;
    return numLines * EditorGUIUtility.singleLineHeight;
}

You might need to play around with it a bit to get what you want. This tells Unity how much space to reserve.

1 Like

Thank you!

Now everything under the spacing isn’t clickable :expressionless: maybe I should override the function that accepts children?

Sorry, I don’t know. It sounds like it’s worth trying, though.

Everything works but the fields under the spacing aren’t clickable.
Edit: Deleted the post by mistake, reproduced.

using UnityEngine;
using UnityEditor;

[CustomPropertyDrawer(typeof(Field))]
public class CustomFieldDrawer : PropertyDrawer {

    SerializedProperty name;
    SerializedProperty prefab;
    SerializedProperty usePrefab;
    SerializedProperty fieldMesh;
    SerializedProperty fieldMaterial;
    SerializedProperty scatter;
    SerializedProperty Parent;

    private int indent;
    private Rect prevPos;

    public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
    {
        Parent    = prop;
        name       = prop.FindPropertyRelative("FieldName");
        prefab    = prop.FindPropertyRelative("Prefab");
        usePrefab = prop.FindPropertyRelative("usePrefab");
        fieldMesh = prop.FindPropertyRelative("FieldMesh");
        fieldMaterial = prop.FindPropertyRelative("FieldMaterial");
        scatter       = prop.FindPropertyRelative("Scatter");

        // prefab override logic works on the entire property.
        EditorGUI.BeginProperty (pos, label, prop);
        pos.xMax -= 4;
        pos.xMin += 4;

        prevPos = pos;

        //Draw label
        pos = EditorGUI.PrefixLabel (new Rect(pos.x/2,pos.y,pos.width,pos.height), GUIUtility.GetControlID (FocusType.Passive), new GUIContent(name.stringValue==""? label.text : name.stringValue));

        indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;

 
    

        int height = 15;

        prop.isExpanded = EditorGUI.Foldout(new Rect(prevPos.x,pos.y,pos.width,pos.height),prop.isExpanded,GUIContent.none);
        if(prop.isExpanded)
        {
            EditorGUI.BeginProperty (pos, label, prop);
            EditorGUI.indentLevel = 1;
            EditorGUI.PropertyField(new Rect(prevPos.x,pos.y+height,prevPos.width,height),name );

            EditorGUI.PropertyField(new Rect(prevPos.x,pos.y+height*2.5f,prevPos.width,height),usePrefab, new GUIContent("Use a Prefab? Must use for network builds"));
     
            if(usePrefab.boolValue)
            {
                EditorGUI.PropertyField(new Rect(prevPos.x,pos.y+height*4f,prevPos.width,height), prefab,new GUIContent("prefab"));

            }
            else
            {

                EditorGUI.PropertyField(new Rect(prevPos.x,pos.y+height*4f,prevPos.width,height),fieldMesh);
                EditorGUI.PropertyField(new Rect(prevPos.x,pos.y+height*5f,prevPos.width,height),fieldMaterial);

            }
     
            EditorGUI.PropertyField(new Rect(prevPos.x,pos.y+height*6f,prevPos.width,pos.height), scatter,new GUIContent("Scattering Properties"),true);


        }

        EditorGUI.EndProperty();
        EditorGUI.indentLevel = indent;
 
    }

    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        int lines=0;
        SerializedProperty RelativeScatter;

        if(Parent!=null && property.propertyType == Parent.propertyType && property.isExpanded)
        {
            lines += 8;
            if((RelativeScatter = property.FindPropertyRelative("Scatter")).isExpanded)
            {
                lines += 6;
            }
            if(RelativeScatter.FindPropertyRelative("UniformScaleRange").isExpanded)
            {
                lines += 2;
            }
        }
        else
        {
            lines += 1;
        }
        if(prevPos.width <= 302)
        {
            lines += 2;
        }
        return lines * EditorGUIUtility.singleLineHeight;
    }



}

I don’t think you need the second BeginProperty() on line 45. If it turns out that you do, remember to match it with an EndProperty() before line 65.

I found my answer here

just set

EditorGUI.Foldout(new Rect(pos.x-2,pos.y,pos.width,16));

I don’t like using stuff like that with hardcoded int values but there doesn’t seem to be another way.

and yes, I didn’t need the second BeginProperty, already deleted :slight_smile:

this is my current script (everything works, need to work on property widths, somehow to keep the propertyfields nicely aligned to the left edge. (anchor maybe?))

using UnityEngine;
using UnityEditor;

[CustomPropertyDrawer(typeof(Field))]
public class CustomFieldDrawer : PropertyDrawer {

    SerializedProperty name;
    SerializedProperty prefab;
    SerializedProperty usePrefab;
    SerializedProperty fieldMesh;
    SerializedProperty fieldMaterial;
    SerializedProperty scatter;
    SerializedProperty Parent;

    private int indent;
    private Rect prevPos;

    public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
    {
        Parent    = prop;
        name      = prop.FindPropertyRelative("FieldName");
        prefab    = prop.FindPropertyRelative("Prefab");
        usePrefab = prop.FindPropertyRelative("usePrefab");
        fieldMesh = prop.FindPropertyRelative("FieldMesh");
        fieldMaterial = prop.FindPropertyRelative("FieldMaterial");
        scatter       = prop.FindPropertyRelative("Scatter");

        // prefab override logic works on the entire property.
        EditorGUI.BeginProperty (pos, label, prop);
        pos.xMax -= 4;
        pos.xMin += 4;

        prevPos = pos;

        //Draw label
        pos = EditorGUI.PrefixLabel (new Rect(pos.x/2,pos.y,pos.width,pos.height), GUIUtility.GetControlID (FocusType.Passive), new GUIContent(name.stringValue==""? label.text : name.stringValue));

        indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;

   
     

        int height = 15;

        prop.isExpanded = EditorGUI.Foldout(new Rect(prevPos.x,pos.y,pos.width,16),prop.isExpanded,GUIContent.none);
        if(prop.isExpanded)
        {
            EditorGUI.indentLevel = 1;
           EditorGUI.PropertyField(new Rect(prevPos.x,pos.y+height,prevPos.width,height),name );

            EditorGUI.PropertyField(new Rect(prevPos.x,pos.y+height*2.5f,prevPos.width,height),usePrefab, new GUIContent("Use a Prefab? Must use for network builds"));
       
            if(usePrefab.boolValue)
            {
                EditorGUI.PropertyField(new Rect(prevPos.x,pos.y+height*4f,prevPos.width,height), prefab,new GUIContent("prefab"));

            }
            else
            {

                EditorGUI.PropertyField(new Rect(prevPos.x,pos.y+height*4f,prevPos.width,height),fieldMesh);
                EditorGUI.PropertyField(new Rect(prevPos.x,pos.y+height*5f,prevPos.width,height),fieldMaterial);

            }
       
            EditorGUI.PropertyField(new Rect(prevPos.x,pos.y+height*6f,prevPos.width,pos.height), scatter,new GUIContent("Scattering Properties"),true);


        }

        EditorGUI.EndProperty();
        EditorGUI.indentLevel = indent;
   
    }

    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        int lines=0;
        SerializedProperty RelativeScatter;

        if(Parent!=null && property.propertyType == Parent.propertyType && property.isExpanded)
        {
            lines += 8;
            if((RelativeScatter = property.FindPropertyRelative("Scatter")).isExpanded)
            {
                lines += 7;
            }
            if(RelativeScatter.FindPropertyRelative("UniformScaleRange").isExpanded)
            {
                lines += 3;
            }
        }
        else
        {
            lines += 1;
       
        }
   
        return lines * EditorGUIUtility.singleLineHeight;
    }



}

Thank you for all of the help :slight_smile:

Happy to provide the little help I did. Thanks for posting your current code. It’ll help others. Heck, I’m going to be working on a couple property drawers today, and I’ll probably come back to your code again myself for some reminders. :slight_smile:

updated the code above,
*Using the Rect from before the PrefixLabel (i.e. prevRect in the code) for the X and width values seems to provide the “Standard” field spacing and placement, everything works and looks like a I want it to.