- Home /
Question by
idbrii · Jul 21, 2020 at 02:18 AM ·
editor-scriptingcustom editoreditorguilayoutattribute
Using EditorGUILayout in my PropertyDrawer for an attribute causes ArgumentException
I have an attribute for strings that does validation and draws a search field to lookup possible values. I wrote it back on 2018.3, but now I'm on 2019.4 and it doesn't work. In fact, nothing with EditorGUILayout seems to work. Even this simple example causes "ArgumentException: Getting control 1's position in a group with only 1 controls when doing repaint":
// Editor/TestStringAttribute_Editor.cs
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System;
using UnityEditor;
using UnityEngine;
namespace TestCode {
// Editor for attribute that provides autocompletion buttons
// [TestString] public string m_Key;
[CustomPropertyDrawer(typeof(TestStringAttribute))]
public class TestStringDrawer : PropertyDrawer {
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
// Add a bit more space for text when narrow.
return EditorGUIUtility.singleLineHeight * (EditorGUIUtility.wideMode ? 1f : 2f);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
var key = property.stringValue;
EditorGUI.BeginChangeCheck();
// This causes "ArgumentException: Getting control 1's position in a group with only 1 controls when doing repaint"
var was_visible = Ehttps://docs.unity3d.com/ScriptReference/PropertyDrawer.htmlditorGUILayout.PropertyField(property, label);
// This works, but I don't want to calculate the position for all my other controls!
//~ var was_visible = EditorGUI.PropertyField(position, property, label);
if (EditorGUI.EndChangeCheck()) {
property.stringValue = key;
}
}
}
}
// TestStringAttribute.cs
using UnityEngine;
namespace TestCode {
// Defines an attribute that provides autocompletion buttons
// See TestStringAttribute_Editor.cs for implementation.
// Use like this:
// [TestString] public string m_Key;
public class TestStringAttribute : PropertyAttribute {}
}
// TestBehaviour.cs
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System;
using UnityEngine;
using Object = UnityEngine.Object;
namespace TestCode {
public class TestBehaviour : MonoBehaviour {
[TestString] public string m_Key;
}
}
How do I make this work?
Comment
Best Answer
Answer by idbrii · Jul 21, 2020 at 02:20 AM
Turns out that even back on 5.6, the documentation says:
Note that for performance reasons, EditorGUILayout functions are not usable with PropertyDrawers.
So it worked by fluke all this time and you have to rewrite using EditorGUI instead of EditorGUILayout.
: (