- Home /
How do I prevent "Argument Exception: Getting control 1's position in a group with only 1 controls when doing repaint" in OnGUI function of my CustomPropertyDrawer?
I am trying to create a CustomPropertyDrawer for a class I've designed called ComponentReference and I am running into some serious issues. I want to use the auto layout system to make things easier on myself (basically using static methods from "EditorGUILayout" instead of "EditorGUI") but this results in the Argument Exception listed in the subject line of this question.
As I understand, this exception is being thrown because there is a mismatch in the controls being laid out in the OnGUI function between function calls. The typical hypothesis I get from other questions on this forum is that the OnGUI function sets up some controls during the "Layout" event, but then something changes and the controls are different when called during the "Repaint" event. So in a segment of code like this:
private bool dropdown;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
foldout = EditorGUILayout.BeginFoldoutHeaderGroup(foldout, label);
if (foldout)
{
EditorGUILayout.LabelField("Hello");
}
EditorGUILayout.EndFoldoutHeaderGroup();
}
One can easily imagine such a scenario occurring here where "foldout" evaluates to "false" when OnGUI is called for the Layout event, but then evaluates to "true" when OnGUI is called during the Repaint phase, which would throw an Argument Exception similar to the one listed in the subject line of this question.
I really believed that this was the case for my own code until actually outputting the event types as they were processed in the OnGUI function of my custom property drawer. Given this code:
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Debug.Log("Current frame num: " + Time.frameCount);
Debug.Log("Current event type: " + Event.current.type);
// Other code
}
The output I got looked like this:
Current frame num: 1079
Current event type: repaint
ArgumentException: Getting control 1's position in a group with only 1 controls when doing repaint
Current frame num: 1080
Current event type: repaint
ArgumentException: Getting control 1's position in a group with only 1 controls when doing repaint
(and so on...)
It's also worth noting that some outputs occurred on the same frame, however, the only event type ever recorded was the "repaint" event. Based on the console outputs, the layout event for OnGUI was never even called for this custom property drawer, which I assume is an issue since I'm trying to use auto-layout.
I've done so much miscellaneous research into this and I'm completely exhausted. The Unity Manual and API is really miserable because it only ever shows examples of CustomPropertyDrawer with manual layouting which is just miserable and completely unacceptable. Here's all the code that makes it happen:
using UnityEngine;
public abstract class ComponentReference where TComponent : Component { [SerializeField] private ComponentReferenceType type; [SerializeField] private GameObject obj; [SerializeField] [TagSelector] private string objectTag; [SerializeField] private bool includeChildren; [SerializeField] private TComponent _component; public TComponent component { get { if (_component == null) { RefreshComponentReference(); } return _component; } } public void RefreshComponentReference() { switch (type) { case ComponentReferenceType.DropTarget: LogErrorIf("No component of type " + typeof(TComponent).ToString() + " found. Did you forget to " + "set the component reference in the editor?", _component == null); break; case ComponentReferenceType.FindInScene: _component = Object.FindObjectOfType(); LogErrorIf("No component of type " + typeof(TComponent).ToString() + " found " + "anywhere in the scene", _component == null); break; case ComponentReferenceType.GameObjectDropTarget: TryGetComponent("No Game Object found. Did you forget to set the Game Object " + "in the editor?"); break; case ComponentReferenceType.TagTarget: obj = GameObject.FindGameObjectWithTag(objectTag); TryGetComponent("No Game Object found with the tag " + objectTag); break; } } private void TryGetComponent(string gameObjectNullMessage) { if (obj != null) { _component = obj.GetComponent(includeChildren); LogErrorIf("No component of type " + typeof(TComponent).ToString() + " found on Game Object with name " + obj.name, _component == null); } else { Debug.LogError(gameObjectNullMessage); } } private void LogErrorIf(string message, bool condition) { if (condition) { Debug.LogError(message); } } }
public enum ComponentReferenceType { DropTarget, FindInScene, GameObjectDropTarget, TagTarget, }
[System.Serializable] public class TransformComponentReference : ComponentReference { }
[System.Serializable] public class CameraComponentReference : ComponentReference { }
// File: ComponentReferenceDrawer using UnityEditor; using UnityEngine;
public class ComponentReferenceDrawer : PropertyDrawer { private bool foldout = false; public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { Debug.Log("Current frame num: " + Time.frameCount); Debug.Log("Current event type: " + Event.current.type); foldout = EditorGUILayout.BeginFoldoutHeaderGroup(foldout, label); // ERROR OCCURS HERE if (foldout) { // Setup the type property SerializedProperty typeProperty = property.FindPropertyRelative("type"); EditorGUILayout.PropertyField(typeProperty); // Change the layout of the serialized properties // based on the enum selected switch (typeProperty.enumValueIndex) { // Property field for the component directly case (int)ComponentReferenceType.DropTarget: EditorGUILayout.PropertyField(property.FindPropertyRelative("_component")); break; // No property fields - the component reference will be located programmatically case (int)ComponentReferenceType.FindInScene: break; // Property field for the game object the component is attached to case (int)ComponentReferenceType.GameObjectDropTarget: EditorGUILayout.PropertyField(property.FindPropertyRelative("obj")); EditorGUILayout.PropertyField(property.FindPropertyRelative("includeChildren")); break; // Property field for a tag of a game object the component is attached to case (int)ComponentReferenceType.TagTarget: EditorGUILayout.PropertyField(property.FindPropertyRelative("objectTag")); EditorGUILayout.PropertyField(property.FindPropertyRelative("includeChildren")); break; } } EditorGUILayout.EndFoldoutHeaderGroup(); } } [CustomPropertyDrawer(typeof(TransformComponentReference))] public class TransformComponentReferenceDrawer : ComponentReferenceDrawer { } [CustomPropertyDrawer(typeof(CameraComponentReference))] public class CameraComponentReferenceDrawer : ComponentReferenceDrawer { }
Sorry if the code looks dumb. This little textbox is being so uncooperative when it comes to formatting the freaking text. Anyways, I'm exhausted so I appreciate anyone's effort to help me out with this
Answer by Ideka · Nov 14, 2019 at 09:37 PM
I found this always happens if:
You use a custom PropertyDrawer that uses
GUILayout
orEditorGUILayout
withinOnGUI
The associated Property decorates a field in a ScriptableObject
The ScriptableObject has no custom Editor
The workaround I found was to just make a custom editor for the ScriptableObject, like so:
[CustomEditor(typeof(Whatever), true)]
public class WhateverEditor : Editor { }
And with that the error goes away.
Thanks a lot, you save my time. WOW. Exactly same case.
Thanks! I had a similar issue, but for the MonoBehaviour and this fixed it
Answer by idbrii · Jul 21, 2020 at 02:27 AM
I want to use the auto layout system to make things easier on myself (basically using static methods from "EditorGUILayout" instead of "EditorGUI") but this results in the Argument Exception listed in the subject line of this question.
I had the same problem and it turns out the documentation for PropertyDrawer says:
Note that for performance reasons, EditorGUILayout functions are not usable with PropertyDrawers.
It used to work for me back on 2018, but it seems that Unity's changed something on 2019 and using EditorGUILayout always causes this ArgumentException.
(Also, it looks like this warning was there even back on 5.6.)
Your answer
Follow this Question
Related Questions
(Solution) - Can't use GUILayout stuff in PropertyDrawer.OnGUI? 2 Answers
Get height of a group of GUILayout controls 1 Answer
Setting height via GUILayout.Height() has no effect to BeginVertical in Inspector 0 Answers
GUI & GUILayout methods no longer work in OnSceneGUI() 1 Answer
How to add multi texture on runtime as "window" array 0 Answers