Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# BeginHorizontal()返回一个空Rect_C#_Unity3d_Unity5 - Fatal编程技术网

C# BeginHorizontal()返回一个空Rect

C# BeginHorizontal()返回一个空Rect,c#,unity3d,unity5,C#,Unity3d,Unity5,所以我想格式化一个我正在处理的自定义EditorWindow,并将所有内容组织在水平布局中,以便元素像表格行一样很好地匹配 不幸的是,我的标题行已关闭,因此我试图通过从EditorGUILayout.BeginHorizontal()方法获取基本Rect来统一所有内容 不幸的是,它返回一个带有默认值的Rect(所有内容都是0)。所以我不能正常使用它。我是否缺少某些内容,或者为什么它返回空的Rect?在编辑器窗口本身中,空间被填满 示例代码: Rect boundary = EditorGUILa

所以我想格式化一个我正在处理的自定义
EditorWindow
,并将所有内容组织在水平布局中,以便元素像表格行一样很好地匹配

不幸的是,我的标题行已关闭,因此我试图通过从
EditorGUILayout.BeginHorizontal()
方法获取基本
Rect
来统一所有内容

不幸的是,它返回一个带有默认值的
Rect
(所有内容都是
0
)。所以我不能正常使用它。我是否缺少某些内容,或者为什么它返回空的
Rect
?在
编辑器窗口
本身中,空间被填满

示例代码:

Rect boundary = EditorGUILayout.BeginHorizontal();
float largeWidth = boundary.width * 0.4f;
float smallWidth = boundary.width * 0.2f;
EditorGUILayout.LabelField("stuff1", GUILayout.Width(largeWidth));
EditorGUILayout.LabelField("stuff2", GUILayout.Width(largeWidth));
EditorGUILayout.LabelField("stuff3", GUILayout.Width(smallwidth));
EditorGUILayout.EndHorizontal();

好吧,我在网上找到了一篇博客文章,上面写着显而易见的:

当对象不知道它将有多大时,不会填充Rect。当触发不包含信息收集的事件时,信息将可用

所以我最后做的是:

  • 我将窗口宽度(始终可用)保存在OnGUI()开头的字段中
  • 我创建了一些常数来处理,这样所有的东西都可以对齐
  • 这些常量只是关于元素之间的间距以及左右填充的假设(我知道这些信息是可用的,但它确实合适,所以我不在乎)
毕竟,这是一个肮脏的解决方案,但它确实起了作用

以下是一些有兴趣的人的示例代码:

public class CustomWindow : EditorWindow
{
    #region Private Fields

    private Vector2 scrollLocation;
    private float elementsWidth;
    private float textBoxWidth;
    private float buttonWidth;
    private const int WINDOW_MIN_SIZE = 400;
    private const int BORDER_SPACING = 10;
    private const int ELEMENT_SPACING = 8;

    #endregion Private Fields

    #region Public Methods

    [MenuItem("Window/CustomWindow")]
    public static void ShowWindow()
    {
        CustomWindow window = GetWindow(typeof(CustomWindow), false, "CustomWindow") as CustomWindow;
        window.Show();
        window.minSize = new Vector2(WINDOW_MIN_SIZE, WINDOW_MIN_SIZE);
        window.Load();
    }

    #endregion Public Methods

    #region Private Methods

    private void OnGUI()
    {
        elementsWidth = EditorGUIUtility.currentViewWidth - BORDER_SPACING * 2;
        textBoxWidth = elementsWidth * 0.4f;
        buttonWidth = elementsWidth * 0.2f;
        scrollLocation = EditorGUILayout.BeginScrollView(scrollLocation);
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("stuff1", GUILayout.Width(largeWidth));
        EditorGUILayout.LabelField("stuff2", GUILayout.Width(largeWidth));
        EditorGUILayout.LabelField("stuff3", GUILayout.Width(smallwidth));
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.EndScrollView();
    }

    #endregion Private Methods
}