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:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class SearchBoxTest : EditorWindow
{
private string onlineSearchTerm = "";
[MenuItem("Window/TEST WINDOW", false, 0)]
protected static void DisplayNugetWindow()
{
GetWindow<SearchBoxTest>();
}
private void OnGUI()
{
EditorGUILayout.BeginVertical();
EditorGUILayout.BeginHorizontal();
{
var oldFontSize = GUI.skin.textField.fontSize;
var oldColor = GUI.skin.textField.normal.textColor;
GUI.skin.textField.fontSize = 20;
if (string.IsNullOrEmpty(onlineSearchTerm))
{
onlineSearchTerm =
EditorGUILayout.TextField("Search", GUILayout.Height(30), GUILayout.ExpandWidth(true));
}
if (onlineSearchTerm == "Search")
{
var placeholderColor = new Color(0.25f, 0.25f, 0.25f);
GUI.skin.textField.normal.textColor = placeholderColor;
GUI.skin.textField.focused.textColor = placeholderColor;
GUI.skin.textField.hover.textColor = placeholderColor;
if (Event.current.type == EventType.MouseDown)
{
GUI.skin.textField.focused.textColor = oldColor;
onlineSearchTerm = "";
GUI.FocusControl("SearchField");
}
}
else
{
GUI.skin.textField.normal.textColor = oldColor;
GUI.skin.textField.focused.textColor = oldColor;
GUI.skin.textField.hover.textColor = oldColor;
}
GUI.SetNextControlName("SearchField");
onlineSearchTerm =
EditorGUILayout.TextField(onlineSearchTerm, GUILayout.Height(30), GUILayout.ExpandWidth(true));
GUI.skin.textField.fontSize = oldFontSize;
GUI.skin.textField.normal.textColor = oldColor;
if (GUILayout.Button("Search", GUILayout.Width(100), GUILayout.Height(28)))
{
// the search button emulates the Enter key
// enterPressed = true;
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.EndVertical();
}
}
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.
(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