Unity Editor GUI文本变得不对齐

时间:2017-03-18 18:58:29

标签: c# user-interface unity3d editor

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

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

enter image description here

以下是问题所涉及的代码

//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 image

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个(仅限用户界面):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    namespace ErrorLoggingSample
    {
     class Program
      {
      static void Main(string[] args)
      {
        try
        {
            string str = string.Empty;
            if (string.IsNullOrEmpty(str))
            {
                throw new Exception("Wrong Data");
            }
        }
        catch (Exception ex)
        {
            ErrorLogging(ex);
            ReadError();
        }

    }

    public static void ErrorLogging(Exception ex)
    {
        string strPath = @"D:\Rekha\Log.txt";
        if (!File.Exists(strPath))
        {
            File.Create(strPath).Dispose();
        }
        using (StreamWriter sw = File.AppendText(strPath))
        {
            sw.WriteLine("=============Error Logging ===========");
            sw.WriteLine("===========Start============= " +       DateTime.Now);
            sw.WriteLine("Error Message: " + ex.Message);
            sw.WriteLine("Stack Trace: " + ex.StackTrace);
            sw.WriteLine("===========End============= " + DateTime.Now);

        }
    }

    public static void ReadError()
    {
        string strPath = @"D:\Rekha\Log.txt";
        using (StreamReader sr = new StreamReader(strPath))
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }
        }
      }
   }
  }

导致此问题的是//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的样式。相反,您应该调用new GUIStyle(EditorStyles.textField)来创建新的TextField对象。