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