Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

[Solved] How to get EditorWindow client rect?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Skyblade, Jun 28, 2019.

  1. Skyblade

    Skyblade

    Joined:
    Nov 19, 2013
    Posts:
    76
    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:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System;
    4.  
    5. public class MyEditorWindow : EditorWindow
    6. {
    7.    Vector2 scrollPosition = Vector2.zero;
    8.  
    9.    [MenuItem("Window/MyEditorWindow")]
    10.    public static void ShowExample()
    11.    {
    12.        var wnd = GetWindow<MyEditorWindow>();
    13.        wnd.titleContent = new GUIContent("MyEditorWindow");
    14.    }
    15.  
    16.    private void OnGUI()
    17.    {
    18.        scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(position.width), GUILayout.Height(position.height));
    19.  
    20.        var elapsed = new TimeSpan();
    21.        for (int i = 0; i < 100; i++)
    22.        {
    23.            elapsed = elapsed.Add(TimeSpan.FromSeconds(1));
    24.        }
    25.  
    26.        for (int i = 0; i < 100; i++)
    27.        {
    28.            GUILayout.Label("Some long long long long text");
    29.        }
    30.  
    31.        GUI.EndScrollView();
    32.    }
    33. }
    What is the proper way of making scrolbars which bound to the edges of the EditorWindow?
     
  2. Skyblade

    Skyblade

    Joined:
    Nov 19, 2013
    Posts:
    76
    Turns out I used `GUI.EndScrollView()`, not `GUILayout.EndScrollView()`.
     
unityunity