Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
12
Question by vexe · Mar 11, 2014 at 05:40 PM · guieditoronguiguilayoutpropertydrawer

(Solution) - Can't use GUILayout stuff in PropertyDrawer.OnGUI?

So, I was writing a property drawer for a type I had, everything went well, nothing out of the ordinary - everything is 'usual' - and by usual I mean I was writing everything using GUILayouts! - I thought oh well, that's nice! they finally added support to GUILayout stuff to property drawers! yay!

So I went ahead and asked a few friends just to confirm it, unfortunately, they didn't experience the same, they had ArgumentExceptions, GUILayoutMismatch and all sorts of nastiness.

I thought it was because they were using an older version of Unity, so I asked them to update, but still, the same results, GUILayout is not working for them, but for me!

This is what I have:

 [CustomPropertyDrawer(typeof(MyType))]
 public class SerializedMBActionDrawer : PropertyDrawer
 {
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         GUILayout.Label("SOMETHING");
     }
 }

 public class MyType : MonoBehaviour { }

Any idea why is this working for me, but not for them?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
24
Best Answer

Answer by vexe · Mar 11, 2014 at 05:51 PM

EDIT: For a better solution altogether, I released my VFW for free. The framework contains a much better drawing API (which lets you write drawers for any object type) with a much better (allocation friendly) and faster GUI layout system.


Well, we made a little debugging session me and @Jamora - it's very obvious that I have something in my project, that is making it work.

So we started out from an empty project - I tried doing the same thing ^ - it didn't work! I got all the nasty stuff he got - and that proves it, there's something in my other project that made it work.

Next step was to import stuff step by step, I kept importing till I reached a point where I imported my Editor folder, and suddenly I got no errors!

So now it's definitely something in the editor folder from my old project, but what is it?

We kept deleting and deleting, util finally we figured it out!

It came down to this:

If you have a custom editor, for the MonoBehaviour that contains an instance of the type you have the drawer for, it will work!

i.e.

 public class Test : MonoBehaviour
 {
    public MyType type;
 }
 
 [CustomEditor(typeof(Test))]
 public class TestEditor : Editor
 {
    // you don't even need to override OnInspectorGUI!
 }
 
 [System.Serializable]
 public class MyType { }
 
 [CustomDrawer(typeof(MyType))]
 public class MyTypeDrawer : PropertyDrawer
 {
    public override void OnGUI(...)
    {
        GUILayout("WHO'S THE TOUGH GUY NOW, HUH?!");
    }
 }

And that's it! :)

I don't know if that's a bug in Unity or not, I don't know if other people know about this, but hey, it works :D

I'm not sure I have an explanation, other than: The property is being drawing inside the custom inspector of Test?

Just thought I'd share.

Let me know if this worked for you or not.

But even if it didn't, we could still make our own GUILayouts - if you look into any of the GUILayout methods, like Button for example you'll notice that the first thing they do, is just get the position of the button, and then call the corresponding GUI method, in this case GUI.Button and pass it the position - if we could get the position ourselves, then it's a piece of cake! - Unfortunately .NET Reflector is acting funny with me at the moment, if I manage to get it running and get my idea to life, I'll post it here as well.

I do not recommend however to 'heavily' depend on this 'hack'? I should say? - Because high chances are this wasn't intended - or maybe it was? who knows... It's subject to change, which if it does, breaks any code that depended on it. That's why it's better for one to make his own GUI wrappers.

Comment
Add comment · Show 7 · 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 Roland1234 · Mar 13, 2014 at 06:00 PM 2
Share

Very interesting - this does actually seem to work provided that, as you say, you have a custom Editor for the type that contains the property, otherwise the use of the layout methods in the property drawer will break it.

The really odd thing is that this will work even if the custom Editor does nothing but use the base implementation for OnInspectorGUI - makes you wonder what it could be doing differently from not defining an Editor at all. Hopefully Unity will soon add official support to layout methods in property drawers, this does make it seem like it could've been supported for some time now.

Good find vexe!

avatar image MOrlando616 · Jan 29, 2015 at 10:50 PM 1
Share

Has anyone discovered the reason for this? I need my class with a custom property drawer to draw itself in normal inspectors...I don't want to have to write a custom editor for every class!

avatar image vexe · Jan 29, 2015 at 10:58 PM 1
Share

@$$anonymous$$Orlando616 the reason? well, the only reason UT gave us is that GUILayout is slow and will cause performance issues if used in PropertyDrawers. So yeah... that's how they solve performance issues :) - I advice you to try VFW, you'll never look back.

avatar image MOrlando616 · Jan 29, 2015 at 11:10 PM 0
Share

I'll have to check that out...I don't think I have the time right now to incorporate any new plugins, but maybe next project. :)

avatar image Glurth · Apr 21, 2016 at 03:50 PM 1
Share

similar question, with details on why having a CustomEditor for the monobehavior container resolves this: http://answers.unity3d.com/questions/1094473/strange-resolution-to-editor-error-explanation-req.html

Show more comments
avatar image
2

Answer by crossfader64 · Sep 07, 2015 at 02:23 PM

@vexe: I stumbled upon this problem, too. But I didn't know that we usually can't use GUILayout. I created a custom Editor and property drawers for my serialized classes. When I added arrays of these classes I wondered, why the hell my array has so much empty space. (See picture) alt text So after debugging I've found out, that if you don't use a custom Editor you'll get many errors when using GUILayout and I think the bug here is, that my custom Editor somehow let the errors disappear. In short: the combination of custom Editor and GUILayout seems to work but I think it is not intended to work. In my case the Editor for my array calculated its size for the amount of elements and because I used GUILayout in the property drawer my elements are getting rendered after the "reserved" space from my array.


example.jpg (16.2 kB)
Comment
Add comment · Show 6 · 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 MadRobot · Jun 13, 2016 at 04:58 PM 0
Share

Any solution on the extra space issue? I've been fighting this for 3 days now. I am using a custom PropertyDrawer to display a serialized class, and I am displaying an array of them inside a custom Editor. This is the ONLY reference I have been able to find about the extra space after the array index and I've spent many hours googling for it.

avatar image crossfader64 MadRobot · Jun 17, 2016 at 08:43 AM 0
Share

@$$anonymous$$adRobot: $$anonymous$$y solution for this was simply not using GUILayout, use GUI ins$$anonymous$$d. I know it's annoying to set the positions manually but at least I got rid of the extra spaces.

avatar image blunzn · Oct 26, 2016 at 07:58 PM 1
Share

Just started with custom editor guis and stumbled across this. I guess its still as confusing as when this thread started...

Anyway i got rid of the spaces in an array of elements, drawn by a CustomPropertyDrawer, by adding

 public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { return 0f; }

Seems that GUI uses this info to add extra spaces, but GUILayout handles height separately.

avatar image blunzn · Oct 26, 2016 at 10:29 PM 1
Share

Just started with custom editor guis and stumbled across this. I guess its still as confusing as when this thread started...

Anyway i got rid of the spaces in an array of elements, drawn by a CustomPropertyDrawer, by adding

 public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { return 0f; }

Seems that GUI uses this info to add extra spaces, but GUILayout handles height separately.

avatar image amirebrahimi_unity · Apr 17, 2018 at 10:21 PM 0
Share

You can get rid of the space by doing:

 GUILayout.Space(-position.height);
avatar image amirebrahimi_unity · Apr 18, 2018 at 07:00 PM 0
Share

Actually, for the above that only works for the first element of an array. It causes issues otherwise. I'd suggest just overriding the height method:

 public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
 {
     return -2f; // this seems to match closest to non-property drawer version
 }

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.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

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? 2 Answers

EditorGUI like light explorer window 1 Answer

How to fix ArgumentException? 3 Answers

Fade out GUILayout Area? 1 Answer

GUILayout not working with Event.Use (Unity 4.0) 0 Answers


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