Unity Editor GUI 文本变得错位

Unity Editor GUI text becoming misaligned

我目前正在开发我的编辑器扩展程序,并决定改为使用图像添加自定义按钮,然后使用水平布局组将其与文本字段放在同一行

不幸的是,文本字段不再与中心对齐。

这里是有问题的代码

//Displays absolute root path
EditorGUILayout.SelectableLabel("Root Directory: " + RootPath, EditorStyles.miniLabel, GUILayout.MaxHeight(16));

//Creates BuildPath
DesiredPathType = (PathType)EditorGUILayout.EnumPopup(new GUIContent("Path Type"), DesiredPathType);

//BuildName TextField
BuildName = EditorGUILayout.TextField(new GUIContent("Build Name"), BuildName);

//OutputPath directory selector
GUILayout.BeginHorizontal();
GUIStyle Style = EditorStyles.textField;
Style.alignment = TextAnchor.UpperLeft;
OutputPath = EditorGUILayout.TextField(new GUIContent("Output Path"), OutputPath, Style);

Style = GUIStyle.none;
Style.padding = new RectOffset(0, 0, 2, 0);
GUILayout.Button(new GUIContent(FolderIcon), Style, GUILayout.MaxHeight(16), GUILayout.MaxWidth(19));

GUILayout.EndHorizontal();

//SubFolders toggle
Subfolders = EditorGUILayout.Toggle(new GUIContent("Subfolder per Platform"), Subfolders);

具体来说,这部分

//OutputPath directory selector
GUILayout.BeginHorizontal();
GUIStyle Style = EditorStyles.textField;
Style.alignment = TextAnchor.UpperLeft;
OutputPath = EditorGUILayout.TextField(new GUIContent("Output Path"), OutputPath, Style);

Style = GUIStyle.none;
Style.padding = new RectOffset(0, 0, 2, 0);
GUILayout.Button(new GUIContent(FolderIcon), Style, GUILayout.MaxHeight(16), GUILayout.MaxWidth(19));

GUILayout.EndHorizontal();

文件夹图标图片

试试这个(仅 UI):

//dummy local variables
string RootPath = null;
PathType DesiredPathType = PathType.Abs;
string BuildName = "";
string OutputPath = "";
bool Subfolders = false;

//Displays absolute root path
EditorGUILayout.SelectableLabel("Root Directory: " + RootPath, EditorStyles.miniLabel, GUILayout.MaxHeight(16));

//Creates BuildPath
EditorGUILayout.EnumPopup("Path Type", DesiredPathType);

//BuildName TextField
BuildName = EditorGUILayout.TextField(new GUIContent("Build Name"), BuildName);

//OutputPath directory selector
GUILayout.BeginHorizontal();
GUIStyle Style = EditorStyles.textField;
Style.alignment = TextAnchor.UpperLeft;
OutputPath = EditorGUILayout.TextField(new GUIContent("Output Path"), OutputPath, Style);

GUILayout.Button(new GUIContent("O"), EditorStyles.label, GUILayout.MaxHeight(16), GUILayout.MaxWidth(19));

GUILayout.EndHorizontal();

//SubFolders toggle
Subfolders = EditorGUILayout.Toggle(new GUIContent("Subfolder per Platform"), Subfolders);

GUIStyle.none 导致了这个问题。上面的代码只是使用 EditorStyles.label 代替。但我不知道为什么会这样。你可以深入EditorGUILayout.Textfield的反编译代码找出原因。

而且您错误地认为您的代码直接修改了 EditorStyles.textField。这将更改所有 TextField 的样式。相反,您应该调用 new GUIStyle(EditorStyles.textField) 来创建一个新的 GUIStyle 对象。