- Home /
How to set EditorGUILayout.PropertyField label column width?
In one of my custom inspectors, I'm iterating over each visible SerializedProperty of an object and using EditorGUILayout.PropertyField to display them in the UI for tweaking, just like a regular inspector would.
This part works fine, but I can't figure out how to override/set the size of the label column. Right now it's eating up way too much space and the value column fields end up very small as a consequence. Using GUILayout.Width() in the PropertyField() call doesn't work as this affects the whole field row, not just the label part.
Does anyone know a way to do this?
I would like to see a code snippet. Are you using a prefix label, the overloaded PropertyField that takes a GUIContent or something else to display the label?
Here's a snippet of the code I'm using to render the property block of the selected node (a $$anonymous$$onoBehaviour type object):
SerializedObject serializedObj = new SerializedObject( selectedNode );
SerializedProperty prop = serializedObj.GetIterator();
while( prop.NextVisible( true ) )
{
if( prop.depth != 0 )
continue;
if( prop.name.EndsWith( "Script" ) )
continue;
EditorGUILayout.PropertyField( prop, true );
}
if( GUI.changed )
serializedObj.Apply$$anonymous$$odifiedProperties();
Answer by spk · Jan 18, 2014 at 04:08 PM
Jamora gave me an idea, so I'll answer my own question :) In the PropertyField doc, it's written you can skip the property label by specifying GUIContent.none, which then allows me to specify my own label and control its size instead:
GUILayout.BeginHorizontal();
GUILayout.Label( prop.name, GUILayout.Width( 75 ) );
GUILayout.FlexibleSpace();
EditorGUILayout.PropertyField( prop, GUIContent.none, true, GUILayout.MinWidth( 100 ) );
GUILayout.EndHorizontal();
I was going to suggest the same thing. If you really want a fancy inspector, remember you can have tootips on almost all gui elements by using GUIContent("element name","tooltip")
ins$$anonymous$$d of just a string.
With this approach, the label can't be used to change the value. There must be another way. How is vec2 implemented for example in UV offset inpsector?
Real heroes are people who post the answer to their own question if they find it! Thank you spk!
Answer by Artifact-Jesse · Dec 09, 2016 at 11:12 PM
This may not have been available when this was originally posted, but a better answer now is probably to use EditorGUIUtility.labelWidth like so:
EditorGUIUtility.labelWidth = 45.0f; // Replace this with any width
EditorGUILayout.PropertyField( someProperty, new GUIContent("Label Text") );
I believe this should solve the problem that @Douvantzis was having.
This will make all the subsequent labels smaller. You may want to reset the width back to default with EditorGUIUtility.labelWidth
.
This is the best solution. Remember to set labelWidth back to default when you're done.
Use this line:
EditorGUIUtility.labelWidth = 0;
This approach has another problem. Let's say your someProperty is Vector3D. Then all its nested labels(X, Y, Z) also will have width of 45f...
You'll need to set the label width for each of the sub-properties via a CustomPropertyDrawer. Here is an example from someone on the Unity forums: https://forum.unity.com/threads/simple-compact-property-drawer.397028/
You should be able to check if the editor is in wide mode or not with EditorGUIUtility.wide$$anonymous$$ode and in the if() statements set your widths when the inspector tab gets too narrow
EDIT: Don't think I understood your concern with the OP but it's still useful xP