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.

Bug EditorWindow TextField Issue: Input Field Shrinks When Empty

Discussion in 'UGUI & TextMesh Pro' started by robrab2000-aa, Sep 12, 2023.

  1. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    115
    I've encountered a peculiar issue with the EditorGUILayout.TextField in Unity's Editor Window. When the onlineSearchTerm string is empty and I click into the TextField, it appears to reduce to half its width. This behavior is inconsistent and doesn't seem to be tied to any specific logic in my code.

    I've provided a stub script below to demonstrate the problem:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    5.  
    6. public class SearchBoxTest : EditorWindow
    7. {
    8.  
    9.     private string onlineSearchTerm = "";
    10.  
    11.     [MenuItem("Window/TEST WINDOW", false, 0)]
    12.     protected static void DisplayNugetWindow()
    13.     {
    14.         GetWindow<SearchBoxTest>();
    15.     }
    16.  
    17.     private void OnGUI()
    18.     {
    19.         EditorGUILayout.BeginVertical();
    20.  
    21.         EditorGUILayout.BeginHorizontal();
    22.         {
    23.             var oldFontSize = GUI.skin.textField.fontSize;
    24.             var oldColor = GUI.skin.textField.normal.textColor;
    25.             GUI.skin.textField.fontSize = 20;
    26.  
    27.             if (string.IsNullOrEmpty(onlineSearchTerm))
    28.             {
    29.                 onlineSearchTerm =
    30.                     EditorGUILayout.TextField("Search", GUILayout.Height(30), GUILayout.ExpandWidth(true));
    31.             }
    32.  
    33.             if (onlineSearchTerm == "Search")
    34.             {
    35.                 var placeholderColor = new Color(0.25f, 0.25f, 0.25f);
    36.                 GUI.skin.textField.normal.textColor = placeholderColor;
    37.                 GUI.skin.textField.focused.textColor = placeholderColor;
    38.                 GUI.skin.textField.hover.textColor = placeholderColor;
    39.  
    40.                 if (Event.current.type == EventType.MouseDown)
    41.                 {
    42.                     GUI.skin.textField.focused.textColor = oldColor;
    43.                     onlineSearchTerm = "";
    44.                     GUI.FocusControl("SearchField");
    45.                 }
    46.             }
    47.             else
    48.             {
    49.                 GUI.skin.textField.normal.textColor = oldColor;
    50.                 GUI.skin.textField.focused.textColor = oldColor;
    51.                 GUI.skin.textField.hover.textColor = oldColor;
    52.             }
    53.  
    54.             GUI.SetNextControlName("SearchField");
    55.             onlineSearchTerm =
    56.                 EditorGUILayout.TextField(onlineSearchTerm, GUILayout.Height(30), GUILayout.ExpandWidth(true));
    57.  
    58.             GUI.skin.textField.fontSize = oldFontSize;
    59.             GUI.skin.textField.normal.textColor = oldColor;
    60.  
    61.             if (GUILayout.Button("Search", GUILayout.Width(100), GUILayout.Height(28)))
    62.             {
    63.                 // the search button emulates the Enter key
    64.                 // enterPressed = true;
    65.             }
    66.         }
    67.  
    68.         EditorGUILayout.EndHorizontal();
    69.  
    70.         EditorGUILayout.EndVertical();
    71.     }
    72. }
    73.  
    I've been able to recreate this issue across multiple Unity versions (all on macOS running on an M1 Max):

    • 2019.4.40f1 (also tested on Windows 11)
    • 2021.3.22f1
    • 2021.3.30f1
    • 2022.3.9f1
    For a clearer understanding, I've attached screenshots that showcase the onlineSearchTerm TextField in its full width and then in its reduced half-width state. If anyone has encountered this before or has any insights into why this might be happening, I'd greatly appreciate your input.

    Screenshot 2023-09-12 at 11.46.50.png Screenshot 2023-09-12 at 11.48.43.png
    (in the second image, I have the left mouse button clicked down within the input field)


    I am planning on submitting this as a bug but I wanted to see if anybody else knew of / had insight on this before doing so.

    Many thanks
     
  2. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    115