2017-07-04 22 views
0

,所以我想格式化定制EditorWindow我的工作和我的水平布局安排一切,让元素很好地适应像表行。EditorGUILayout.BeginHorizo​​ntal()返回一个空矩形

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

不幸的是,它返回默认值Rect(一切都是0)。所以我无法正确使用它。我缺少的东西,或为什么它返回一个空Rect?在EditorWindow本身,空间已满。

示例代码:

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(); 

回答

1

好了,我发现了一个博客网上的文章,说明明显:

的矩形将不会被填补,当对象不不,怎么会大是。当发生不包含信息收集的事件时,信息将可用。

所以我落得这样做是:

  • 我保存在一个领域的窗口宽度(这是可以在所有的时间)在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 
}