0
\$\begingroup\$

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.

EditorWindow

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?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Well, don't overwrite the width with your own value... :p

In my example I Use EditorGUILayout instead of GUILayout, but in this case that won't make a difference. The below code makes the scrollrect fill the entire window.

scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);

for (int i = 0; i < 100; i++)
{
    GUILayout.Label("Some long long long long text");
}

EditorGUILayout.EndScrollView();

In case you want to get the actual width of the window, see EditorGUIUtility.currentViewWidth

\$\endgroup\$
2
  • \$\begingroup\$ Thank you for the answer, it's a good workaround, but the reason and fix was even simpler :) \$\endgroup\$
    – Skyblade
    Commented Jun 28, 2019 at 15:24
  • \$\begingroup\$ True, I did miss the fact that you actually wrote GUI instead of GUILayout. But you still don't need to specify a width at all though \$\endgroup\$
    – troien
    Commented Jun 28, 2019 at 15:31
0
\$\begingroup\$

Turns out I used GUI.EndScrollView(), not GUILayout.EndScrollView().

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .