• 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 /
This question was closed Feb 04, 2018 at 02:46 AM by expat1999 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by expat1999 · Jan 21, 2018 at 07:39 AM · inspectoreditor-scriptingonguipropertydrawerongui error

EditorGUILayout.Foldout not working properly - results in argument exceptions

:

For starters, I have successfully created a CustomPropertyDrawer that presents an also functioning custom property type.

:

The problem occurs when I try to create a Foldout that contains everything that that CustomPropertyDrawer does within OnGUI.

:

I followed the information on these links:

  1. EditorGUILayout.Foldout documentation

  2. A related Unity Answer that backs up the documentation

:

 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
 {
         showMatrix = EditorGUILayout.Foldout(showMatrix, "Matrix");
 
         if (showMatrix)
         {
             // custom property code
         }
 }

:

Above is exactly what my code looks like (I even tried commenting out the "custom property code" that you see in the code snippet above, to no avail).

:

So, my question is, why is this not working? Is it a bug? Or is this outdated and I need to find a new solution?

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jan 22, 2018 at 12:46 AM

Propertydrawers can't use GUILayout or EditorGUILayout stuff. A PropertyDrawer gets a rect that it has to use. Everything you draw has to fit into the "position" rect. If you need more space for your property you have to override the GetPropertyHeight method and return the required height.


Again you can't use any layout functions since the default inspector doesn't even propergate the layout event to property drawers.

Comment
Add comment · Show 4 · 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 expat1999 · Jan 22, 2018 at 01:39 AM 0
Share

So... could I, say use a "collapsed" bool field to get the same effect as the EditorGUILayout.Foldout?

:

Like this:
:
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
SerializedProperty = property.FindPropertyRelative("collapsed");
EditorGUI.PropertyField(newPosition, collapsed, GUIContent.none);

              if (!collapsed.boolValue)
              {
                  // custom property code
              }
      }

Unfortunately... the "code sample" above is not working properly...

:

Edit: Just tried this -- it functions properly, however it treats the entire property that appears when false as a button. A$$anonymous$$A Anywhere I click in the matrix that I have made appear closes it again. Thoughts?

:

I will mark this as the best answer, seeing as you did technically answer the original question.

avatar image Bunny83 expat1999 · Jan 22, 2018 at 05:05 AM 1
Share

Uhm, your code seems a bit messed up and contains many things that aren't declared / initialized anywhere. You don't need to define a collapsed field. Any SerializedProperty with children has an isExpanded field which you can use for exact this purpose.


You also don't need to use a PropertyField. It's fine to use an EditorGUI.Foldout. It's just the layout variant which can't be used inside a property drawer. Of course you have to distribute your available space (the position rect) yourself. Something like this:

 Rect foldoutRect = position;
 foldoutRect.height = 15f;
 position.y$$anonymous$$in = foldoutRect.y$$anonymous$$ax;

This will basically "cut off" 15 pixels from the original property space. The foldoutRect represents that top area which you can use for your foldout. The remaining position rect would be the space below.

avatar image expat1999 Bunny83 · Jan 23, 2018 at 03:56 AM 0
Share

Yes, my code snippet got screwed up. Regardless the version I posted did not work. And yeah, I kinda figured it was rather convoluted way of doing things.

:

In response to your comment -- I simply changed EditorGUI.Foldout ins$$anonymous$$d of EditorGUILayout.Foldout and everything is working exactly as needed... thanks!

avatar image villevli · Jan 22, 2018 at 07:50 AM 0
Share

This may be a bit of a hack but I think that GUILayout starts working in PropertyDrawers if you add a class like this to your project:

 [CustomEditor(typeof(UnityEngine.Object), true)]
 public class UnityObjectEditor : Editor
 {
 }

You can also replace UnityEngine.Object with $$anonymous$$onoBehaviour or ScriptableObject.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

84 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 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 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

How to get width at which Inspector does line breaks (for Vectors) 1 Answer

How to show type text in EditorGUI.ObjectField? 1 Answer

Unity won't let me override the default way of drawing an array/list? 2 Answers

Callback when custom PropertyAttribute changes 0 Answers

Why is my property drawer not drawing on the next line? 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