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 · Aug 25, 2017 at 07:02 PM · sizecustom editorpositioningpropertydrawer

How to prevent EditorGUI.indent from overriding property height (only)

I have a simple property drawer, which draws a single object field, for a Texture object. Both the ObjectField height, and the property height, used by this custom property drawer, are static. Here are the relevant property drawer lines, from inside OnGUI. (Will post full custom property drawer upon request- its nothing special though) …

 position.width = 80;
 position.height = 80;
         
 Texture2D temp_tex =  (Texture2D) EditorGUI.ObjectField(position, GUIContent.none, tex, typeof(Texture2D), false);

…

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

However, when I adjust the indent level, before drawing the property, it appears to affect the HIEGHT of the drawn ObjectField. But I expected "indent" to only affect the width, and x position of the position Rect.

 public override void OnInspectorGUI()
     {
         EditorGUILayout.PropertyField(serializedObject.FindProperty("tester"));
         Rect position = EditorGUILayout.GetControlRect(true, 100);
         EditorGUI.indentLevel++;
         EditorGUI.PropertyField(position, serializedObject.FindProperty("tester"));
         EditorGUI.indentLevel--;
     }

Yields: alt text Note that the drawn height of the two ObjectFields, is different. The one that is drawn after increasing the indent, is displayed smaller than the 80 pixels hard-coded (let me repeat that in bold: hard-coded) in the property drawer.

How do I prevent adjustments to the indent level, from affecting height? (but still affect horizontal position, as normal) Do I actually need to reset the Indent Level to zero, apply my own changes to the rect width and x position, then change it back (possible, but a huge pain, and conflicts with advice on bottom of EditorGUI.indentLevel docs page)?

Setting the EditorGUIUtility.hierarchyMode flag to false, didn’t help : http://answers.unity3d.com/questions/1320999/editorguifoldout-docs-wrong.html

inentaffectsheight.png (21.3 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

2 Replies

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

Answer by Adam-Mechtley · Aug 28, 2017 at 03:42 PM

For the sake of posterity, I wanted to share what I sent back from the bug report:

Thanks for submitting! Now that I can see all of your code in context, I can confirm this is not in fact a Unity bug.

The problem is that when you use the various EditorGUI methods, they will respect the current indent level within the rect where you draw them. What happens is that your indent level inside of your ObjectField rect (the one with the large preview swatch) is still 1 at the time it is drawn, and Unity draws the swatch shorter so that it maintains the correct aspect ratio. So in this case, it is actually correct to manually set the indent level to 0 before you draw your ObjectField, and then reset it afterward.

Comment
Add comment · Show 5 · 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 · Aug 28, 2017 at 05:20 PM 0
Share

Thanks for the update Adam. So, even though the EditorGUI-indentLevel documentation page specifically states that I should NOT compute my own indent, you are saying that IS what I need to do? Does unity provide the number of pixels per indent level, or should I hard code it?

"Unity draws the swatch shorter so that it maintains the correct aspect ratio." Oh, good to know.

$$anonymous$$ore generally, how do I know when a control is going to use the RECT I provide, and when it is going to change that rect, (and how the rect will be changed e.g. just Rect.x$$anonymous$$in, or height also)?

Just venting frustration: This kinda unexpected/undocumented behavior/apparent-inconsistency is making it very difficult to get my property drawers laid out right (not my first issue with this stuff)- screens that should take few hours seems to always end up taking days. ( here is another example http://answers.unity3d.com/questions/1320999/editorguifoldout-docs-wrong.html) If the layout behavior was properly documented, I wouldn't have a leg to stand on here, but this stuff is NOT documented: discovering undocumented behaviors like this is VERY FRUSTRATING. It leads to hours of doubting myself, testing, researching, and preparing a question for Unity Answers, and possibly submitting bugs (which wastes YOUR time too). (I know docs is NOT your department Adam, but if you could pass on my concerns/frustrations, to someone who CAN make a difference there, It would really be appreciated.)

avatar image Adam-Mechtley Glurth · Aug 28, 2017 at 06:04 PM 1
Share

I'm assu$$anonymous$$g you're referring to this part of the the EditorGUI.indentLevel documentation:

To maximize future compatibility, do not make assumptions about what a specific indent level means, but ins$$anonymous$$d just increase or decrease by one around blocks of controls that need to be more indented, as in the example above.

What this text means is that if you need to indent something, do it by changing this property rather than manually adjusting pixels on the rect that you supply to an EditorGUI call.

That said, it sounds like there are sort of 3 key pieces of information that you couldn't find (below). If you have specific suggestions of some obvious place or places this information should have been more explicit, I'd be happy to pass it along.

  1. You should then expect that any control that is drawn via an EditorGUI (or EditorGUILayout) method will having indenting applied based on the current indentLevel. The idea is that EditorGUI methods are still using your rect, they are just indenting the control within that rect. The ObjectField large preview swatch just has the peculiarity that it maintains a 1:1 aspect ratio, so the indentation ends up also affecting the height in the case that you have no label (i.e. are using GUIContent.none for the label).

  2. $$anonymous$$ost EditorGUI methods draw a row that consists of a label and a corresponding control, and are intended to be draw row-by-row. When there is a label present, the label is indented but the controls will all align along the same horizontal position. Where there is no label (i.e. you use GUIContent.none) then the control will span the entire indented rect. (There are some specific controls without labels, like reorderable list and serializable events, that currently do not respect indent level, but those should be considered bugs.)

  3. When you are using something like a property drawer, you need to remember point number 1. There is some indentLevel while your property is being drawn that may not be 0 (especially if your item is in an array or list), so if you are doing some kind of custom layout like yours, you must remember to adjust indentLevel within your PropertyDrawer's OnGUI() method accordingly.

    [1]: https://docs.unity3d.com/ScriptReference/EditorGUIUtility-fieldWidth.html

avatar image Glurth Adam-Mechtley · Aug 30, 2017 at 04:48 PM 0
Share

Thank you, Adam, those details are very useful!

"suggestions of some obvious place"

I would suggest the general aspects of number 1&2&3, should be somewhere under the EditorGUI Class page: Since it refers to the various EditorGUI member functions that take a RECT as a parameter, and the critical information needed is exactly how the RECT, IndentLevel (and any other factors) will be used/modified internally, to draw the control.

It might be worth it to create a "Rect Rules"(awful name, I know) subsection, kinda like the "Shader" area of the documentation does. I'd also suggest an exceptions sub-page that includes the aspect-ratio stuff for ObjectField, along with any others controls with exceptions to the Rect Rules laid out, like (perhaps?) Foldout.

A simple link to it this info, on each EditorGUI-taking-a-Rect-function page, and on the IndentLevel page, would help people find this info during a normal function "look-up" in the docs. Personally, I'm a fan of sample code and pictures.

Show more comments
avatar image
1

Answer by Adam-Mechtley · Aug 25, 2017 at 07:20 PM

Hi! This is a bug with how ObjectField works with Texture assets. Could you please file a report? It should be a super simple, 3-line fix, but I want to make sure we track it. If you post the case number I'd even be happy to take care of it Monday :)

That said, in the meantime, you're right this is the best workaround:

reset the Indent Level to zero, apply my own changes to the rect width and x position, then change it back

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 · Aug 25, 2017 at 08:03 PM 0
Share

Thanks, Adam! I have submitted a bug report. Case number is: 945452 "take care of it $$anonymous$$onday" Woah, you mean like: fix the Unity Editor? Cool! So then it would be available whenever the next patch after that comes out?

avatar image Adam-Mechtley Glurth · Aug 25, 2017 at 11:51 PM 0
Share

Thanks for reporting. I see case 945452, which looks like yours.

If I can fix it $$anonymous$$onday, I make no guarantees how quickly it will get into a patch release, as it still has to go through some hoops to get into a release. For example, I can also see it looks like you're on 5.6.x, so it would have to make it into a newer version first before it is eligible for back porting. I can post here when I know for sure.

avatar image Glurth Adam-Mechtley · Aug 26, 2017 at 01:28 AM 0
Share

"back porting"?! ::sputters:: ... but ... oh .. ::clicks download:: Thanks again, Adam!

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

70 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

Related Questions

How to properly deal with EditorGUI.indentLevel in custom PropertyDrawers 1 Answer

Inspector Overlapping Text Label at a Position 1 Answer

Draw on Multiple Lines with EditorGUI 1 Answer

SerializedObject.FindProperty returning null 2 Answers

How can I check SerializedPropertyChangeEvent to see if changedProperty is valid? 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