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 /
  • Help Room /
avatar image
0
Question by breakeren · Apr 09, 2018 at 09:11 AM · editorguipropertydrawer

indentation problems when drawing nested properties with Property Drawer

I have this Class called ToggledFloat. It wraps a float but also adds a bool telling if it is enabled or not.

 [System.Serializable]
 public class ToggledFloat
 {
     public float value;
     public bool enabled = false;
 }
 
 [System.Serializable]
 public class ClassWIthNestedProp
 {
     public ToggledFloat aTogFloat;
 }
 
 public class Test : MonoBehaviour
 {
     public ClassWIthNestedProp eCA;
     public ToggledFloat tF;
 }

I can easily make a Custom Property Drawer for this, and it looks right, when the editor draws this property at indentation level "0". However when I look at ToggledFloat properties nested inside another property they look wrong.

My propertyDrawer looks like this:

 [CustomPropertyDrawer(typeof(ToggledFloat))]
 public class ToggledPropertyEditor : PropertyDrawer
 {
     
     public override float GetPropertyHeight (SerializedProperty property, GUIContent label)
     {
         var propVal = property.FindPropertyRelative("value");
         float contentUnfoldedHeight = EditorGUI.GetPropertyHeight (propVal, label);
         return contentUnfoldedHeight;
     }
 
     // Draw the property inside the given rect
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         int iL = EditorGUI.indentLevel;
         //EditorGUI.indentLevel = 0; //- Set this to "0" to make thing work but non-indented
         SerializedProperty valueProp = property.FindPropertyRelative("value");
         SerializedProperty enabledProp = property.FindPropertyRelative("enabled");
 
         //Label left of checkbox
         float labelWidth = GUI.skin.label.CalcSize(label).x;
         Rect labelRect = new Rect(0/*position.x*/, position.y, labelWidth, 16);
         EditorGUI.LabelField(labelRect, label, GUIContent.none);
         EditorGUI.DrawRect(labelRect, new Color(0,0,1,.1f));
 
         //Checkbox
         Rect enableRect = new Rect();
         enableRect.xMin = labelRect.xMax;
         enableRect.yMin = labelRect.yMin;
         enableRect.width = 16;
         enableRect.height = 16;
         EditorGUI.PropertyField(enableRect, enabledProp, GUIContent.none);
         EditorGUI.DrawRect(enableRect, new Color(0,1,0,.1f));
 
         //Value
         Rect valueRect = new Rect();
         valueRect.xMin = enableRect.xMax;
         valueRect.yMin = enableRect.yMin;
         valueRect.xMax = position.xMax;
         valueRect.yMax = position.yMax;
 
         EditorGUI.DrawRect(valueRect, new Color(1,0,0,.1f));
         bool enabled = GUI.enabled;
         GUI.enabled = enabledProp.boolValue;
         EditorGUI.PropertyField(valueRect, valueProp, new GUIContent(""), true);
         GUI.enabled = enabled;
         EditorGUI.indentLevel = iL;
 
     }
 }

When the inspector draws an instance of class Test, it looks like this: alt text

The Colored rects are only there for me to debug, where those rects actually are. The weird thing is that the text-labels are offset from the coloured rects, even though they get the same rect as argument. This is of course only a problem if I want coloured rects in my inspector - but the problem is that it seems that this offset problem causes nested checkboxes to not work. I cannot click checkboxes on a nested property.

If I then explicitly set EditorGUI.IndenLevel = 0, then the coloured rects and the labels coincide and the toggle buttons work properly - but I then loose the automatic indentation, that I would really like to use. alt text

Can someone tell me what I am overlooking

screen-shot-2018-04-09-at-110724.png (16.2 kB)
screen-shot-2018-04-09-at-110109.png (16.1 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

Answer by Deadcow_ · Aug 28, 2019 at 01:01 PM

I encountered the same problem and figured out how to fix this.

Unity applies indent to every function in EditorGUI that uses rect, like any text field and toggle etc. So to keep indent and fix this issue you need to draw first gui item with indent and set indent to 0 after that:

 position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
 EditorGUI.indentLevel = 0;
 // alter position
 EditorGUI.PropertyField(position, minProp, GUIContent.none);
 // alter position
 EditorGUI.Toggle(position, isSet, GUIContent.none);
Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

130 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 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 spacing between inspector property labels and fields? 0 Answers

EditorGui.PropertyField, how do I reduce the label's width ? 1 Answer

Components can finally be rearranged with the mouse, but since when? 1 Answer

How to draw a model in the preview window of my custom editor using OnPreviewGUI? 0 Answers

Drawing a Mesh from an EditorTool 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