Buttons inline with Properties in custom PropertyDrawer

Hey guys!

I’m looking for a method to display buttons inline (or better in the next line) of my custom PropertyDrawer. But I was not able to figure out yet how to do it.

2949167--218609--gesture.PNG

As you can see, the buttons belonging to a single property, are drawn below the last property and the GUILayout.Space is also applied here. For my understanding, an issue could be that EditorGUI.BeginProperty(…) / .EndProperty is not compatible with GUILayout. But I don’t see another option to add buttons to the PropertyDrawer…

The code:

using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(TrainingGestureData))]
public class TrainingGestureDataDrawer : PropertyDrawer
{
    public enum placeholder
    {
        oneHanded,
        twoHanded
    }

    private placeholder handiness;

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        SerializedProperty gestureId = property.FindPropertyRelative("GestureID");
        SerializedProperty gestureName = property.FindPropertyRelative("GestureName");
        SerializedProperty inputData = property.FindPropertyRelative("InputData");


        EditorGUI.BeginProperty(position, label, property);

        int ident = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;

        // Calculate rects
        Rect labelRect = new Rect(position.x, position.y, 20, position.height);
        Rect nameRect = new Rect(position.x + 25, position.y, 150, position.height);
        Rect handinessRect = new Rect(position.x + 180, position.y, 100, position.height);
        Rect numberRect = new Rect(position.x + 285, position.y, 120, position.height);

        // Draw fields (use GUIContent.none to disable label)
        EditorGUI.LabelField(labelRect, gestureId.intValue.ToString());
        EditorGUI.PropertyField(nameRect, gestureName, GUIContent.none);
        handiness = (placeholder)EditorGUI.EnumPopup(handinessRect, handiness);
        EditorGUI.LabelField(numberRect, "Recorded inputs: " + inputData.arraySize);

        Rect buttonsRect = EditorGUILayout.BeginHorizontal();

        // Get object
        object trainingGestureObj = fieldInfo.GetValue(property.serializedObject.targetObject);

        if (GUILayout.Button("Train Gesture"))
        {
            Debug.Log(gestureId.intValue);
        }
        GUILayout.Button("Remove Last Input");
        GUILayout.Button("Clear All Inputs");

        EditorGUILayout.EndHorizontal();

        EditorGUI.indentLevel = ident;

        EditorGUI.EndProperty();

        GUILayout.Space(10);
    }
}

Btw: Is it correct, that GUIEditor, etc. is just supported as legacy code? If yes, what’s the “new way” to create PropertyDrawers then?

You can’t use GUILayout or EditorGUILayout in Property Drawers, you have to calculate rects for the buttons and use GUI.Button(rect, label);

GUIEditor, ect is supported as legacy code for make ingame UI but not for editor.

6 Likes