Error using BeginHorizontal in CustomPropertyDrawer

I’m trying to align fields horizontaly in a custom drawer but I get this error:

ArgumentException: GUILayout:
Mismatched LayoutGroup.Repaint
UnityEngine.GUILayoutUtility.BeginLayoutGroup
(UnityEngine.GUIStyle style,
UnityEngine.GUILayoutOption options,
System.Type LayoutType) (at
C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/GUILayoutUtility.cs:210)
UnityEditor.EditorGUILayout.BeginHorizontal
(UnityEngine.GUIContent content,
UnityEngine.GUIStyle style,
UnityEngine.GUILayoutOption options)
(at
C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/EditorGUI.cs:5854)
UnityEditor.EditorGUILayout.BeginHorizontal
(UnityEngine.GUILayoutOption
options) (at
C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/EditorGUI.cs:5833)
LevelInfoEditor.OnGUI (Rect position,
UnityEditor.SerializedProperty
property, UnityEngine.GUIContent
label) (at
Assets/Scripts/Editor/LevelInfoEditor.cs:22)
UnityEditor.PropertyDrawer.OnGUISafe
(Rect position,
UnityEditor.SerializedProperty
property, UnityEngine.GUIContent
label) (at
C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/ScriptAttributeGUI/PropertyDrawer.cs:23)
UnityEditor.PropertyHandler.OnGUI
(Rect position,
UnityEditor.SerializedProperty
property, UnityEngine.GUIContent
label, Boolean includeChildren) (at
C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/ScriptAttributeGUI/PropertyHandler.cs:134)
UnityEditor.EditorGUI.PropertyFieldInternal
(Rect position,
UnityEditor.SerializedProperty
property, UnityEngine.GUIContent
label, Boolean includeChildren) (at
C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/EditorGUI.cs:4363)
UnityEditor.EditorGUI.PropertyField
(Rect position,
UnityEditor.SerializedProperty
property, Boolean includeChildren) (at
C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/EditorGUIBindings.cs:698)
UnityEditor.EditorGUI.PropertyField
(Rect position,
UnityEditor.SerializedProperty
property) (at
C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/EditorGUIBindings.cs:693)
UnityEditor.Editor.OptimizedInspectorGUIImplementation
(Rect contentRect) (at
C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/EditorBindings.cs:202)
UnityEditor.GenericInspector.OnOptimizedInspectorGUI
(Rect contentRect) (at
C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/Inspector/GenericInspector.cs:46)
UnityEditor.InspectorWindow.DrawEditor
(UnityEditor.Editor editor, Int32
editorIndex, Boolean forceDirty,
System.Boolean&
showImportedObjectBarNext,
UnityEngine.Rect&
importedObjectBarRect, Boolean
eyeDropperDirty) (at
C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/Inspector/InspectorWindow.cs:1069)
UnityEditor.InspectorWindow.DrawEditors
(UnityEditor.Editor editors) (at
C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/Inspector/InspectorWindow.cs:903)
UnityEditor.InspectorWindow.OnGUI ()
(at
C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/Inspector/InspectorWindow.cs:332)
System.Reflection.MonoMethod.Invoke
(System.Object obj, BindingFlags
invokeAttr, System.Reflection.Binder
binder, System.Object parameters,
System.Globalization.CultureInfo
culture) (at
/Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)

My class:

[CustomPropertyDrawer(typeof(SpawnInfo))]
public class LevelInfoEditor : PropertyDrawer
{
    public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
    {
        //base.OnGUI(position, property, label);

        EditorGUILayout.BeginHorizontal(); // error!
        EditorGUILayout.EndHorizontal();
    }
}

You can’t use Unity’s Layout system in Property drawers :slight_smile: - You have to use GUI/EditorGUI and define exactly how tall your drawer is in GetHeight.

Yes, Unity wants us to write painful code having us manually positioning things and dealing with raw rects. That’s how they solve “performance issues” :slight_smile: (which is their excuse for not letting us use the Layout system in property drawers) If you’re interested in a better deal, I’ve written a faster layouting system and a better property drawing system where you could use both GUI/GUILayout. Check out VFW

There, you could write:

public class SpawnInfoDrawer : ObjectDrawer<SpawnInfo>
{
   public override void OnGUI ()
   {
       using (gui.Horizontal())
       {
          // your horizontal code...
       }
   }
}