- Home /
How to fix ArgumentException?
Hi,
I'm getting this error:
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:5847)
UnityEditor.EditorGUILayout.BeginHorizontal (UnityEngine.GUILayoutOption[] options) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/EditorGUI.cs:5826)
UnityEditor.InspectorWindow.AddComponentButton (UnityEditor.Editor[] editors) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/Inspector/InspectorWindow.cs:1208)
UnityEditor.InspectorWindow.OnGUI () (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/Inspector/InspectorWindow.cs:359)
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)
While using this code:
[CustomPropertyDrawer(typeof(Test))]
class TestDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (Event.current.type == EventType.Repaint || Event.current.type == EventType.Layout)
{
GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
base.OnGUI(position, property, label);
}
}
}
And it doesn't draw out properly, it just glitches the whole inspector.
I tried switching out the GUILayout.Box
for GUI.skin.box.draw
and got it to work, but it didn't maintain the layout like I intend it to do.
I've been stuck on this for a couple hours, attempting to Google/search the Forum/Answers for a fix, but I couldn't find one. All the answers that I found didn't work for me.
Does anyone know of a fix for this? If so, how do I fix it?
Thank you,
KaliTech.
EDIT: I found a temporary fix(which I dislike, so I still need help) by increasing the position manually. It's unreliable for my situation, but it works for now I guess.
Thanks.
EDIT: Here's what I'm left with after Bunny83's suggestion:
And with this code:
[CustomPropertyDrawer(typeof(Test))]
class TestDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
EditorGUILayout.Space();
base.OnGUI(position, property, label);
}
}
Answer by KaliTech · Oct 11, 2014 at 03:36 AM
I found a solution(thanks for trying, Bunny83):
float extraSpace = 5;
Rect newPropertyRect = new Rect(position.x, position.y + extraSpace, position.width, position.height);
EditorGUI.PropertyField(newPropertyRect, property, label);
Answer by Bunny83 · Oct 10, 2014 at 03:28 AM
Is there any good reason why you filter all events but repaint and layout? This will most likely cause tons of problems as every event is paired with a layout event. Since you let the layout events through but are blocking all others (except repaint) Unity might have trouble when iterating through it's internal list of controls.
Also if you want more space for your property, you should override PropertyDrawer.GetPropertyHeight as well.
If you just want a space in the layouting system you should use GUILayout.Space which is ment for that purpose.
So in general you should be careful when filtering events. In your case you blocked all keyboard and mouse events. If you want to prevent interaction, just set GUI.enabled to false.
Hi,
The reason why I did that is because it was throwing other errors before I used the "filter." I will try your suggestion and report back the results.
Ok, this error happens exactly when you mess up the calling of opening a layout group and closing them (ie: BeginHorizontal and EndHorizontal). Do you have any other property drawers or Editors where you also do such strange even filtering? You somehow have managed to mess up the internal layout stack.
When using GUILayout, unity collects information about all controls during the layout event. In the following event it's impportant to use the exact same controls. This error happens when Unity tries to fetch the record for a layout element from the stack, but the stack is empty. This usually happens when you have more EndXXX than BeginXXX calls. $$anonymous$$ake sure they always match.
btw: what is "Test"? a custom serializable class or a custom attribute class? If it's a custon serializable class, does the $$anonymous$$onoBehaviour that contains that class also have a CustomEditor?
It's hard to impossible to figure out what's wrong with the given information. The only thing that seems to be quite certain is that you probably have more EndXXX than BeginXXX calls somewhere.
Just remembered that property drawers don't use GUILayout at all. They give you a fix rect for the property to draw in. This rect is deter$$anonymous$$ed by the layout system + the value returned by GetPropertyHeight. So the available space and position is fix, you can't change that. The only thing you can influence is the height of your property field by overriding GetPropertyHeight.
If you want to offset the default drawer, you have to change the position manually. If you really want to use the layout system for this simple thing, you have to start a new layout group with GUILayout.BeginArea and GUILayout.EndArea. $$anonymous$$eep in $$anonymous$$d that calling the base OnGUI method again requires a valid rect, so you might want to do something like:
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return base.GetPropertyHeight(property, label) + 2;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
GUILayout.BeginArea(position);
GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
Rect newPropertyRect = GUILayoutUtility.GetRect(label, "label",GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
GUILayout.EndArea();
base.OnGUI(newPropertyRect, property, label);
}
While such an approach usually works, it's way easier and faster to offset the rect manually. $$anonymous$$eep in $$anonymous$$d that all GUILayout functions call the GUI equivalent in the end. It just adds a GetRect call in font to get a rect from the layout system.
Answer by idbrii · Jul 21, 2020 at 02:28 AM
I had the same ArgumentException 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.)
That's why Bunny's solution helps.
Your answer
Follow this Question
Related Questions
Handling Undo/REdo in EditorWindow when editing DB record. 0 Answers
EditorGUI like light explorer window 1 Answer
Drawing several BeginArea inside a BeginScrollview (GUILayout) produces an unexpected behaviour 1 Answer
Create Multiple Foldouts. 1 Answer
Script work in editor but not in build 0 Answers