【问题标题】:Unity Editor GUI text becoming misalignedUnity Editor GUI 文本变得未对齐
【发布时间】:2017-08-10 06:07:27
【问题描述】:

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

很遗憾,文本字段不再与中心对齐。

这是有问题的代码

//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.EndHorizo​​ntal();

文件夹图标图片

【问题讨论】:

  • 文本字段停止与中心对齐?预期的外观是什么?也许你可以给我们看一张草图。
  • 在左侧添加问题,在右侧添加问题
  • 我仍然无法弄清楚问题所在。左右唯一的区别是FolderIcon的存在感。
  • 放大并仔细观察;右侧的输出路径文本字段在其上方和下方的 GUI 元素之间完美居中,而在左侧,整个内容略微太低
  • 哦,我明白了。

标签: c# user-interface unity3d editor


【解决方案1】:

试试这个(仅限 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 对象。

【讨论】:

  • 似乎或多或少地工作。图片最终太小了,我不喜欢,但我可能会解决这个问题
猜你喜欢
  • 2014-08-06
  • 2020-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多