- Home /
How to get EditorWindow client rect?
I want to create a simple console-like custom EditorWindow in Unity. There is a bunch of lables (console lines).
I'm using GUILayout.Begi/EndScrollView to position all the labels in one column. The problem is that it places all the content inside the rectangle that matches label bounds, not EditowWindow bounds. As a result, I see weird placed scrollbars.
I would be happy to recalculate ScrollView bounds, but can't find any clue in documentation how to make so. Here is my code:
using UnityEditor;
using UnityEngine;
using System;
public class MyEditorWindow : EditorWindow
{
Vector2 scrollPosition = Vector2.zero;
[MenuItem("Window/MyEditorWindow")]
public static void ShowExample()
{
var wnd = GetWindow<MyEditorWindow>();
wnd.titleContent = new GUIContent("MyEditorWindow");
}
private void OnGUI()
{
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(position.width), GUILayout.Height(position.height));
var elapsed = new TimeSpan();
for (int i = 0; i < 100; i++)
{
elapsed = elapsed.Add(TimeSpan.FromSeconds(1));
}
for (int i = 0; i < 100; i++)
{
GUILayout.Label("Some long long long long text");
}
GUI.EndScrollView();
}
}
What is the proper way of making scrolbars which bound to the edges of the EditorWindow?
Answer by Bunny83 · Jun 28, 2019 at 02:45 PM
Well, the layout system can only distribute the area that is available to the current layout group.
For some reason the automatic top layout group has a fix width. Just wrap your code with a GUILayout area:
GUILayout.BeginArea(new Rect(0,0,position.width, position.height));
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
// [ ... ]
GUI.EndScrollView();
GUILayout.EndArea();
edit
Ahh I just realised your mistake ^^. You used GUI.EndScrollView
and not GUILayout.EndScrollView
. That's why the layouted rect is completely wrong. So just do
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
// [ ... ]
GUILayout.EndScrollView();
And your scrollview should cover the whole window.