- Home /
Draw on Multiple Lines with EditorGUI
I am using property drawers to expose some custom classes in the Unity inspector.
So far so good, except I have a class which I would like to draw over multiple lines. I understand that Unity doesn't allow us to use EditorGUILayout calls within property drawers so I've been trying to make this draw correctly using EditorGUI functions instead.
The problem I'm having is that I can't seem to figure out drawing on multiple lines. If I change the Y position or height of the rect for the EditorGUI call then the element will move but the inspector doesn't resize to accommodate this. If I do nothing then the elements overlap.
What I want:
What I have:
If anybody can give me some pointers on how to create a property drawer that extends over multiple lines like in the example above, I would be very greatful!
Thanks in advance!
Answer by Loui_Studios · Oct 30, 2020 at 03:49 PM
Ok so I managed to find a solution hiding in the documentation.
Basically, in order to draw on multiple lines with EditorGUI() you have to use the GUILayoutUtility.GetRect() function. It reserves space on the inspector for your element and also returns the rect you can use to draw it.
Here's an example of usage:
Rect rect = GUILayoutUtility.GetRect(GUIContent.none, EditorStyles.objectField);
EditorGUI.PropertyField(rect, location, GUIContent.none);
Hope this clears up any issues for others!
Your answer
Follow this Question
Related Questions
Inspector Overlapping Text Label at a Position 1 Answer
Property Drawer SerializedProperty is null 2 Answers
Property Drawer and Children 3 Answers
How to get the target object value from a PropertyDrawer for an array of objects? 1 Answer
How to properly deal with EditorGUI.indentLevel in custom PropertyDrawers 1 Answer