Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Glurth · Mar 03, 2017 at 04:26 PM · editorrectdocumentationpropertydrawer

EditorGUI.Foldout: docs wrong?

I have created a very simple property drawer to test this: I use the position passed into the property drawer to both draw a foldout, and to draw a box styled label. The same position RECT is used to draw both, and I expected the result to look similar to the position as defined in the docs: alt text

Note that in this image (from https://docs.unity3d.com/ScriptReference/EditorGUI.Foldout.html), the position.xMin is to the left of the triangle indicator.

However, my tests show the position differently! In this test, the position.XMin is to the RIGHT of the triangle indicator. So, it appears, the indicator is NOT actually drawn inside the specified position Rect. alt text

Below is the entirety of my test project. Can someone else confirm they get the same results, and/or explain what I'm misunderstanding?

TestMono.cs

 using UnityEngine;
 
 [System.Serializable]
 public class PropTestClass
 {
     string a;
 }
 public class TestMono : MonoBehaviour {
     public PropTestClass p;
 }

Editor\ TestMonoPropertDrawer.cs

 using UnityEngine;
 using UnityEditor;
 
 [CustomPropertyDrawer(typeof(PropTestClass))]
 public class TestMonoPropertDrawer : PropertyDrawer
 {
     
     bool showDetailsFlag;
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         Debug.Log("foldout position" + position.ToString());
         showDetailsFlag = EditorGUI.Foldout(position, showDetailsFlag, new GUIContent("test Label"), false);
         Debug.Log("box position" + position.ToString());
         EditorGUI.LabelField(position, GUIContent.none, GUI.skin.box);
     }
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return 20;
     }
 }

Edit: Both Debug.Log calls, in the property drawer, show the exact same position Rect value.

myeditorguifoldout.jpg (11.1 kB)
editorguifoldout.png (15.5 kB)
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

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

Answer by Bunny83 · Mar 03, 2017 at 07:31 PM

Well, it's not entirely "wrong". That's because the Foldout behaves differently depending on the state of "EditorGUIUtility.hierarchyMode". If hierarchyMode is true it adds the padding of the foldoutstyle to the left of the given position. If it's false it does not. The following lines are right at the beginning of the Foldout method:

 if (EditorGUIUtility.hierarchyMode)
 {
     int num = EditorStyles.foldout.padding.left - EditorStyles.label.padding.left;
     position.xMin -= (float)num;
 }

The inspector is pretty much the only one who is setting the hierarchyMode to true. In most other cases if should be false.

So yes, then used in a custom inspector it will offset itself to the left to give space for the "triangle". If rendered anywhere else (like in the example they rendered it in a custom EditorWindow) the triangle would be inside the given rect.

So the docs aren't wrong, but missed to mention that polymorph behaviour ^^.

Comment
Add comment · 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 Glurth · Mar 04, 2017 at 03:47 PM 0
Share

You are much more generous than me: I would have to classify that position box in the docs as "just-plain-wrong".

Good idea to look up the decompiled code (sad that it is necessary)! I followed your lead on that, and found some more "position mucking with" stuff, in that same function down where it handles repaint events. Apparently, it ALSO adjusts the position using GUI.indent.

So it looks like, if one wants the control to draw in the ACTUAL position RECT passed to the function, without additional layout formatting (like the pic in the docs): one must manually set:

 EditorGUI.indet=0;

and

 EditorGUIUtility.hierarchy$$anonymous$$ode= false;

before calling the EditorGUI.Layout function. Does that sound right to you @Bunny83? Any others I missed?

Just to clarify: "The inspector is pretty much the only one who is setting the hierarchy$$anonymous$$ode to true." So, this is the case for all custom PropertyDrawers and all CustomEditors? Or do I misunderstand? Thanks, as always, for your help!

avatar image Bunny83 Glurth · Mar 04, 2017 at 05:06 PM 1
Share

Yes, everything that is called from the inspector of course is also affected. So yes it includes all Editors and PropertyDrawers.

I think the point here is that the Inspector has this "reserved" column at the front. The actual usable space starts to the right of that column. Inside that "column" the Foldout (any maybe some other controls) draw those "expanding" handles, but only when drawn from the inspector.

As i said this "special behaviour" just isn't documented. The example they used in the docs didn't use the inspector but an editor window. So everything is correct in the docs-example (at least this time ^^).

btw: EditorGUI.indent is internal and a read-only property:

 internal static float indent { get { return (float)EditorGUI.indentLevel * 15f; } }

indentLevel on the other hand can be set. It usually is set to 0. Only when the inspector is showing nested objects (like custom serializable classes or array items) it will increase it for each nested level. When you mess around with those in a PropertyDrawer you might want to store the current values and reset them at the end of your code.

There is even an internal class that does this ^^. "UnityEditorInternal.VR.PlayerSettingsEditorVR" inside the "DrawVRDeviceElement" method they have:

 bool hierarchy$$anonymous$$ode = EditorGUIUtility.hierarchy$$anonymous$$ode;
 EditorGUIUtility.hierarchy$$anonymous$$ode = false;
 vRCustomOptions.IsExpanded = EditorGUI.Foldout(position, vRCustomOptions.IsExpanded, "", false, EditorStyles.foldout);
 EditorGUIUtility.hierarchy$$anonymous$$ode = hierarchy$$anonymous$$ode;


btw: That's the perfect example why global state is evil ^^. Some static state that is read somewhere inside a little nested helper function changes the behaviour and you have no idea what's going on ^^.

avatar image Glurth Bunny83 · Mar 04, 2017 at 06:12 PM 0
Share

"That's the perfect example why global state is evil ^^. Some static state that is read somewhere inside a little nested helper function changes the behaviour and you have no idea what's going on ^^"

Here here! For a while, I was sitting there wondering why I couldn't do basic addition anymore.

Hmm, so is that "reserved" column you mentioned defined by EditorStyles.label.padding.left? Looking at the EditorGUI source code, I do not see where the other control types, like say.. Label, are applying that offset (nor the indent offset). (Even when I try to account for the above stuff, something seems to be messing up positions if I do NOT override those two state values: starting to suspect this "reserved" space.)

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

77 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

Related Questions

GetPropertyHeight infinite recursion on drawer 1 Answer

What is Selection.SetActiveObjectWithContext for? 4 Answers

Drawing a custom variable in the inspector y position bugs selection 0 Answers

Customizing Inspector variables. 2 Answers

PropertyDrawer not painted when scrolling upwards 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