Search Unity

Much too large space between label and field. How to fix?

Discussion in 'Immediate Mode GUI (IMGUI)' started by jerotas, Mar 23, 2015.

  1. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Hi, I have a custom editor script that needs to show more than one control in a row, so it has to use a "Horizontal" to accommodate. The problem I'm having is that the distance between "X" and the int field is very large and I haven't been able to find any way to shorten it. Notice that's like at least 100 pixels "extra" that I can't seem to get rid of. If I put minWidth of 120 or less, text textbox looks messed up and tiny. Changing MinWidth does not shorten the distance between label and int field. It looks like this at the shortest (below).

    upload_2015-3-22_22-11-43.png

    Here's the code.
    Code (csharp):
    1.  
    2.   EditorGUILayout.BeginHorizontal();
    3.   var newVal = EditorGUILayout.IntField("X", killInt.selfValue, GUILayout.MinWidth(180));
    4.   var newSource = (LevelSettings.VariableSource)EditorGUILayout.EnumPopup(killInt.variableSource, GUILayout.Width(70));
    5.   EditorGUILayout.EndHorizontal();
    6.  
    I know I could use GUILayout.Label to do the label separately (and closer), but that disables the "drag behavior" of the label where it changes the value as you drag left and right on the label, and I need that on this control for visualization that happens with any change. Any ideas? I know I've seen custom inspectors with labels closer than this.
     
    Last edited: Mar 23, 2015
    SonicCane11 and MilenaRocha like this.
  2. Mr-Mud

    Mr-Mud

    Joined:
    Mar 8, 2015
    Posts:
    37
    Assigning a new value to EditorGUIUtilty.labelWidth will change the width of the labels. So I suppose this is what you are looking for.
     
  3. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    Hey there,

    The default for labels and most controls expand width is equal to true.

    Try adding
    Code (CSharp):
    1. GUILayout.ExpandWidth (false)
    That should solve your problem. The label will now only take up as much space as needed.
     
  4. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    You're awesome! It works.
     
    hsyed_unity likes this.
  5. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    This one didn't work. Thanks for the post though!
     
    lclemens likes this.
  6. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    That is because you have min width set. ExpandWidth(false) says shrink to your smallest size that your content allows. MinWidth sets the minimum size it can shrink too.

    Using Width(X) just overrides MinWidth so there is no real point using both.
    1. var newVal = EditorGUILayout.IntField("X", killInt.selfValue, GUILayout.ExpandWidth(false));
     
  7. G4M5T3R

    G4M5T3R

    Joined:
    Jan 10, 2015
    Posts:
    10
    Necro: I'm having similar issues with ExpandWidth() not working as described.
    Code (CSharp):
    1. var num = EditorGUILayout.IntField("#", id.number, GUILayout.ExpandWidth(false));
    Idk why... I'm not using any of the other layout options like min/max and the logic above for expand makes perfect sense, but I'm new to custom editors so can't really provide any insight. Just thought I'd share because I too had to resort to manually setting the labelWidth...

    *Edit: I've been playing with the parameters and have narrowed it down to the Horizontal container...sorta. Without it Expand is working but only on the IntField itself. It does not seem to affect the spacing between the field and the label though. Is GUIStyle the trick here? I tried messing with GUIstyle.margin to no avail...
     
    Last edited: Nov 15, 2019
  8. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Seems like Horizontal layout breaks the no-expand logic. Most likely the case.

    If anyone knows how to workaround this, please post your solution.
     
  9. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    I've just encountered this too, it seems that the most useful usage case, i.e. in a horizontal layout, is the only time it doesn't work!
     
  10. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
  11. KingsHere

    KingsHere

    Joined:
    Nov 3, 2015
    Posts:
    3
    For the Horizontal Layout this thing worked for me ‍:

    Code (CSharp):
    1. EditorGUILayout.BeginHorizontal();
    2.  
    3. //Your checkbox and Label code
    4.  
    5. GUILayout.FlexibleSpace();
    6. EditorGUILayout.EndHorizontal();
    Before :
    upload_2020-7-24_13-27-48.png

    After :
    upload_2020-7-24_13-27-13.png
     
    Last edited: Jul 24, 2020
    Munchy2007 likes this.
  12. KnightWhoSaysNi

    KnightWhoSaysNi

    Joined:
    Jan 21, 2017
    Posts:
    13
    ExpandWidth(false) didn't work
    adding FlexibleSpace didn't work
    the only thing that works for me is setting MaxWidth to some value, which just seems wrong
     
  13. UsefulYdyot

    UsefulYdyot

    Joined:
    Sep 20, 2014
    Posts:
    3
    Cant it be that the intent is a problem?
    Had a similar issue and setting the intentLevel to 0 in front of the elements that were pushed too far to the right, fixed it.

    Code (CSharp):
    1. EditorGUI.indentLevel = 0;
     
  14. Boof

    Boof

    Joined:
    Aug 11, 2014
    Posts:
    10
    None of these solutions seem to work anymore on Unity 2021.3.3f1 :/
     
    Celezt, zcyandyaurora and SonicCane11 like this.
  15. GarrettF

    GarrettF

    Joined:
    Jul 4, 2019
    Posts:
    49
    Just incase anyone is asking Michael Googlay (googling around) about this, as I have:

    Using Begin Horizontal and flexible space did not fix the issue with too much space between the field and the field name.

    EditorGUIUtilty.labelWidth worked perfectly.

    I have that inside of a Horizontal Layout block with flexible space at the end.
     
    Liam2349 and Florian-G like this.
  16. HunterAhlquist

    HunterAhlquist

    Joined:
    Oct 6, 2013
    Posts:
    132
    None of the solutions here work for me in 2020.3
     
  17. GoodMinso

    GoodMinso

    Joined:
    Feb 14, 2022
    Posts:
    1
    same, its kind of frustrating
     
  18. a1creator

    a1creator

    Joined:
    Jan 18, 2021
    Posts:
    24
    Code (CSharp):
    1. EditorGUI.indentLevel = 0;
    2.             script.resX = EditorGUILayout.IntField("Width:", script.resX, GUILayout.ExpandWidth(false));
    3.             script.resY = EditorGUILayout.IntField("Height:", script.resY, GUILayout.ExpandWidth(false));
    4.             GUILayout.FlexibleSpace();
    2021.3.12f1 here. Applied all mentioned fixes and nothing changes. (This is inside of a Horizontal group)
     
    lclemens likes this.
  19. drawingghost

    drawingghost

    Joined:
    May 2, 2017
    Posts:
    1
    Try This way:

    EditorGUILayout.BeginHorizontal();
    EditorGUIUtility.labelWidth = 10;
    demoValue.Demo_Int_1 = EditorGUILayout.IntField("X",demoValue.Demo_Int_1);
    demoValue.Demo_Int_2 = EditorGUILayout.IntField("Y", demoValue.Demo_Int_2);
    demoValue.Demo_Int_3 = EditorGUILayout.IntField("Z", demoValue.Demo_Int_3);
    EditorGUILayout.EndHorizontal();
     
    Celezt likes this.
  20. Thomas-Bousquet

    Thomas-Bousquet

    Joined:
    Dec 19, 2016
    Posts:
    41
    So if you're trying to get a result like this:
    upload_2022-12-7_15-16-54.png
    Then you can use the following code, that create the label separately and make it as small as its content.
    Code (CSharp):
    1. enum VariableSource{Self}
    2. int killInt = 1;
    3. VariableSource variableSource = VariableSource.Self;
    4.  
    5. private void OnGUI()
    6. {
    7.     EditorGUILayout.BeginHorizontal();
    8.     TightLabel("X");
    9.     killInt = EditorGUILayout.IntField(killInt);
    10.     variableSource = (VariableSource)EditorGUILayout.EnumPopup(variableSource, GUILayout.Width(70));
    11.     EditorGUILayout.EndHorizontal();
    12. }
    13.  
    14. public static void TightLabel(string labelStr)
    15. {
    16.     GUIContent label = new GUIContent(labelStr);
    17.     //This is the important bit, we set the width to the calculated width of the content in the GUIStyle of the control
    18.     EditorGUILayout.LabelField(label, GUILayout.Width(GUI.skin.label.CalcSize(label).x));
    19. }
     
    oen, kira0x1, Hozgen90 and 1 other person like this.
  21. PhlCrn

    PhlCrn

    Joined:
    Jun 8, 2023
    Posts:
    1
    EditorGUIUtility.labelWidth = 150;
    EditorGUILayout.PropertyField(serializedObject.FindProperty("my_var"));

    and adapt the 150 value at your will. It is just the easiest way :)
     
  22. leodar163

    leodar163

    Joined:
    Jun 5, 2019
    Posts:
    3
    It works just as espected, thanks Thomas
     
    vstefaniuk-n3twork likes this.
  23. kira0x1

    kira0x1

    Joined:
    Jun 9, 2015
    Posts:
    1
    thank you this is great <3
     
    vstefaniuk-n3twork likes this.