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
0
Question by RevCosmosis · Mar 05, 2017 at 10:11 PM · editorinspectoreditorguiserializedproperty

Creating vertical space for EditorGUI

My problem has to do with using EditorGUI (as opposed to EditorGUILayout because I want pretty strict control over the sizing and positioning of elements). I got the GUI working as I want it, but I can't figure out how to tell Unity how tall the element as a whole is.

Here's a screenshot of a ScriptableWizard displaying the SerializedProperty:

pic

I figured out how to cheese the system by adding a bunch of EditorGUILayout blank space at the end based on the height of the UI (ypos is a variable derived from the loops used to generate the interface):

 if (Event.current.type == EventType.Layout) {
     EditorGUILayout.BeginVertical();
     for (int i = 0; i < ypos; i += 5) EditorGUILayout.Space();
     EditorGUILayout.EndVertical();
 }

pic

However, once Unity tries to display this interface in the inspector instead of a wizard, my cheese method stops working. It displays on top of other elements.

My question is more or less this: What's the non-cheese way of doing this? Here's the complete class I'm working with:

 [CustomPropertyDrawer(typeof(PuzzleGrid))]
 public class PuzzleGridDrawer : PropertyDrawer {
     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
         EditorGUI.BeginProperty(position, label, property);
 
         var indent = EditorGUI.indentLevel;
         EditorGUI.indentLevel = 0;
 
        //UI Code using EditorGUI and GUI.Button exclusively, hidden because it's like 150 lines and shouldn't be relevant. Let me know if it is and I can include the whole thing.
 
         EditorGUI.indentLevel = indent;
 
         EditorGUI.EndProperty();
 
         if (Event.current.type == EventType.Layout) {
             EditorGUILayout.BeginVertical();
             for (int i = 0; i < ypos; i += 5) EditorGUILayout.Space();
             EditorGUILayout.EndVertical();
         }
     }
 }

(I have tried setting position.height directly, but it doesn't do anything.)

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
1

Answer by Bunny83 · Mar 06, 2017 at 02:57 AM

A PropertyDrawer has exactly two method which you can override:

  • OnGUI

  • GetPropertyHeight

Unity first calls "GetPropertyHeight" of your PropertyDrawer to determine the height of your property. After that it calls OnGUI with the proper position rect that has the requested height.

Maybe i got something wrong about your question, but overriding GetPropertyHeight and providing the correct height should fix your problem. The default property height is 16 pixels. GetPropertyHeight is literally implemented like this:

 public virtual float GetPropertyHeight(SerializedProperty property, GUIContent label)
 {
     return 16f;
 }

So just override the method.

edit
ps: A property drawer should always stay within its position rect. Your code doesn't show how you actually draw your GUI, but i guess you draw outside the rect.

Comment
Add comment · Show 1 · 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 Adam-Mechtley · Mar 06, 2017 at 09:53 AM 1
Share

Just to add to @Bunny83's answer, you should not call Editor/GUILayout methods inside a PropertyDrawer, as these are not supported.

avatar image
1

Answer by JHobsie · Mar 06, 2017 at 06:00 PM

If you have a dynamic element then you can set the 'GetPropertyHeight' to be variable depending on number of children or if it is expanded etc, so the next element will always be directly below it.

Here's an example of how to determine a list objects height. Assuming each line is the default height (Which you can get from EditorGUIUtility.singleLineHeight which is 16)

 [SerializeField]
 ListObject listObj;
 
 [Serializable]
 public class ListObject
 {
     public List<int> myList = new List<int>();
 }
 
 [CustomPropertyDrawer(typeof(ListObject))]
 public class ListObjectPropertyDrawer : PropertyDrawer
 {
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         float height = base.GetPropertyHeight(property, label);
         SerializedProperty listProperty = property.FindPropertyRelative("myList");
         if(listProperty.isExpanded)
         {
             // foldout and size field + children
             height *= 2 + listProperty.arraySize;
         }
 
         return height;
     }
 
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         position.height = EditorGUIUtility.singleLineHeight;
 
         SerializedProperty listProperty = property.FindPropertyRelative("myList");
 
         listProperty.isExpanded = EditorGUI.Foldout(position, listProperty.isExpanded, label);
         position.y += position.height;
         if(listProperty.isExpanded)
         {
             EditorGUI.indentLevel++;
             listProperty.arraySize = EditorGUI.IntField(position, "Size", listProperty.arraySize);
             position.y += position.height;
 
             for(int i = 0; i < listProperty.arraySize; i++)
             {
                 SerializedProperty element = listProperty.GetArrayElementAtIndex(i);
                 EditorGUI.PropertyField(position, element);
                 position.y += position.height;
             }
             EditorGUI.indentLevel--;
         }
     }
 }

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

81 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

Related Questions

How to properly handle Undo events in custom inspector? 0 Answers

Initialising List array for use in a custom Editor 1 Answer

Insert new custom class element with _default_ values to a SerializedProperty array? 1 Answer

EditorGUILayout.ObjectField cannot be changed. 1 Answer

How to show type text in EditorGUI.ObjectField? 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