- Home /
PropertyDrawer always errors when calling OnInspectorGUI
I'm trying to write a PropertyDrawer that will expand nested ScriptableObjects, but even my minimal attempt fails:
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(ScriptableObject), true)]
public class ScriptableObjectDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.PropertyField(position, property, label, true);
var editor = Editor.CreateEditor(property.objectReferenceValue, null);
editor.OnInspectorGUI();
}
}
I'm testing on a ScriptableObject with an array of ScriptableObjects as one of its properties. I get an error on line 13: "Getting control 4's position in a group with only 4 controls when doing Repaint".
I'm not sure how to debug this any further.
Answer by Bunny83 · Oct 19, 2017 at 09:55 PM
This doesn't work. PropertyDrawers work with a fix Rect while custom editors work with the layout system. By default the Layout event is not propagated to PropertyDrawers, so you can't even create a new layout group as the layoutsystem won't work in this case.
However if you actually create a custom inspector for the inspector that is using your property drawer, the layout event is getting through. However it's generally not supported.
Even if you create such a property drawer, you have to override the GetPropertyHeight method to reserve enough space for your whole "property".
If you want some fancy specialized editor for your class you may want to create a seperate editor window.
Here's an older question on a similar topic
Thanks. I was having trouble digging through all the old threads and piecing together what's true for current-day Unity.
For others in the same situation: I ended up eschewing the PropertyDrawer and writing a full editor.