Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

remove empty space between the EditorGUILayout.TextField's label and inputbox

Discussion in 'Immediate Mode GUI (IMGUI)' started by Zurusona-Kitsune, May 6, 2013.

  1. Zurusona-Kitsune

    Zurusona-Kitsune

    Joined:
    Mar 11, 2012
    Posts:
    94
    Hi.
    I am trying to create an editorwindow menu but I have a problem with the EditorGUILayout.TextField.

    My code is something like this:

    Code (csharp):
    1.  
    2.  
    3. void OnGUI ()
    4. {
    5.  EditorGUILayout.BeginHorizontal();
    6.  {
    7.   EditorGUILayout.TextField("mytextfield", value);
    8.   GUILayout.Space(10);
    9.   EditorGUILayout.Toggle("myToggle", true);
    10.  }
    11.  EditorGUILayout.EndHorizontal();
    12. }
    13.  
    As you see, something very simple.
    I can control the space between the Textfiled and the toggle by changing the GUILayout.Space(x) line but I cannot find where to change the space beween the EditorGUILayout.TextField's label and inputbox.
    Same problem with the toggle's name and checkbox too.
    It looks like there is 2 tabulations inbetween but I would like to remove this.
     
  2. vikingfabian-com

    vikingfabian-com

    Joined:
    Dec 5, 2013
    Posts:
    9
    You can use this method to get the label size auto adjusted

    Code (CSharp):
    1. /// <summary>
    2.   /// Create a EditorGUILayout.TextField with no space between label and text field
    3.   /// </summary>
    4.   public static string TextField(string label, string text)
    5.   {
    6.   var textDimensions = GUI.skin.label.CalcSize(new GUIContent(label));
    7.   EditorGUIUtility.labelWidth = textDimensions.x;
    8.   return EditorGUILayout.TextField(label, text);
    9.   }
     
    magique likes this.
  3. Zurusona-Kitsune

    Zurusona-Kitsune

    Joined:
    Mar 11, 2012
    Posts:
    94
    thanks. I'll use your code to update mine.
     
  4. bankle

    bankle

    Joined:
    Jul 8, 2018
    Posts:
    1
    Thanks,It's solved my problem.
     
  5. asd4568uy

    asd4568uy

    Joined:
    Jun 25, 2021
    Posts:
    1
    It is very useful
     
  6. mrVentures

    mrVentures

    Joined:
    Nov 11, 2018
    Posts:
    9
    Thanks, this worked well for me. And to control the width of the input field you can add a third parameter to the return statement as show here:
    Code (CSharp):
    1. return EditorGUILayout.TextField( label, text,  GUILayout.Width( 400 ) );
     
  7. mrVentures

    mrVentures

    Joined:
    Nov 11, 2018
    Posts:
    9
    Beware, I observed you need to reset the value via
    Code (CSharp):
    1.  EditorGUIUtility.labelWidth = -1;
    Otherwise all future labels within the inspector will have the width of your first label.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    I moved this necroed thread from 2014 to the correct forum (IMGUI).