Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
9
Question by spk · Jan 18, 2014 at 02:13 PM · editorguipropertyfield

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?

Comment
Add comment · Show 2
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 Jamora · Jan 18, 2014 at 02:36 PM 1
Share

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?

avatar image spk · Jan 18, 2014 at 04:01 PM 0
Share

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();

2 Replies

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

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();
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 Jamora · Jan 18, 2014 at 05:10 PM 0
Share

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.

avatar image Douvantzis · Apr 16, 2016 at 03:38 PM 0
Share

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?

avatar image Deniz2014 · Apr 05, 2017 at 10:48 AM 0
Share

Real heroes are people who post the answer to their own question if they find it! Thank you spk!

avatar image
8

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.

Comment
Add comment · Show 6 · 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 Dio1990 · Jan 24, 2017 at 03:27 AM 0
Share

Thank you, the probelm solved.

avatar image blade_sk · Apr 16, 2018 at 02:13 PM 0
Share

This will make all the subsequent labels smaller. You may want to reset the width back to default with EditorGUIUtility.labelWidth.

avatar image Xanatos_88 · May 04, 2018 at 07:02 PM 0
Share

This is the best solution. Remember to set labelWidth back to default when you're done.

Use this line:

 EditorGUIUtility.labelWidth = 0;

avatar image papirosnik · Jun 26, 2018 at 06:42 AM 0
Share

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...

avatar image Artifact-Jesse papirosnik · Jun 26, 2018 at 03:06 PM 0
Share

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/

avatar image Ian-Gungee papirosnik · Jun 27, 2018 at 03:00 AM 1
Share

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

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

28 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

Related Questions

EditorGUILayout.PropertyField in Custom EditorWindow Shows Wrong Text On Focus 1 Answer

Change look of one type to different everywhere 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Should EditorGUILayout.PropertyField work with serializable classes? 1 Answer

How do I add a drop-down menu to an EditorWindow? 3 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