Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

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,499
    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,499
    You're awesome! It works.
     
    hsyed_unity likes this.
  5. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,499
    This one didn't work. Thanks for the post though!
     
  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:
    2,919
    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,701
    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:
    488
  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:
    9
    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:
    5
    None of these solutions seem to work anymore on Unity 2021.3.3f1 :/
     
    zcyandyaurora and SonicCane11 like this.
  15. GarrettF

    GarrettF

    Joined:
    Jul 4, 2019
    Posts:
    43
    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.
     
    Florian-G likes this.
  16. HunterAhlquist

    HunterAhlquist

    Joined:
    Oct 6, 2013
    Posts:
    83
    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. a1-creator

    a1-creator

    Joined:
    Jan 18, 2021
    Posts:
    5
    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)
     
  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();