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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. 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

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:
    5
    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:
    5
    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:
    8,618
    I moved this necroed thread from 2014 to the correct forum (IMGUI).