• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by KaliTech · Oct 10, 2014 at 01:30 AM · c#guieditorguilayout

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: Picture

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);
         }
     }

capture1.png (285.8 kB)
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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);
Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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.

Comment
KaliTech

People who like this

1 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KaliTech · Oct 10, 2014 at 03:31 AM 0
Share

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.

avatar image Bunny83 · Oct 10, 2014 at 05:40 AM 1
Share

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. Make 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 MonoBehaviour 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.

avatar image Bunny83 · Oct 10, 2014 at 06:09 AM 0
Share

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 determined 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. Keep in mind 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. Keep in mind 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.

avatar image

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.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make GUILayout horizontal first, then vertical? (C#) 3 Answers

TextField does not show up 2 Answers

Why is my SerializedProperty.objectReferenceValue Not Accepting my Object? 0 Answers

Dynamic GUI Columns in GUI Area? (C#) 1 Answer

GUILayout Context Menu on Right Click - Not Working! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges