Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with scroll bars in an Editor Window

Discussion in 'Immediate Mode GUI (IMGUI)' started by Stickworm, Oct 13, 2013.

  1. Stickworm

    Stickworm

    Joined:
    Apr 24, 2011
    Posts:
    67
    Hi I'm trying to add scroll bars to my editor window so I can effectively look around a graph I'll be making. Here I've made a comically large button to represent the graph. I am unable to use the scroll bars to scroll along the length of the button despite the fact that the Area Rect and button is too large to view, what am I doing wrong?

    Thanks for any help.


    Code (csharp):
    1. public class DemoWindow: EditorWindow{
    2.  
    3.     Vector2 scrollPos = Vector2.zero;
    4.  
    5.         void OnGUI(){
    6.  
    7.         scrollPos = EditorGUILayout.BeginScrollView(scrollPos, true, true, GUILayout.Width(window.position.width), GUILayout.Height(window.position.height));
    8.  
    9.         GUILayout.BeginArea(new Rect(0, 0, 1500, 1500));
    10.         GUILayout.Button("Test", GUILayout.Width(1500));
    11.         GUILayout.EndArea();
    12.         EditorGUILayout.EndScrollView();
    13.  
    14.     }
    15. }
     
    TheDeveloper10 likes this.
  2. Stickworm

    Stickworm

    Joined:
    Apr 24, 2011
    Posts:
    67
    The problem seems to be with having a GUILayout Area with in the scroll view, without the GUI Area then the scroll bars will function correctly.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestScrollView : MonoBehaviour {
    5.    
    6.     Vector2 scrollPosition = Vector2.zero;
    7.    
    8.     void OnGUI(){
    9.         scrollPosition = GUILayout.BeginScrollView(scrollPosition, true, true,  GUILayout.Width(100),  GUILayout.Height(100)); 
    10.         //GUILayout.BeginArea(new Rect(0, 0, 300, 300));    //Does not display correctly if this is not commented out!
    11.        
    12.         GUILayout.Button("I am a button", GUILayout.MinWidth(150), GUILayout.MinHeight(150));
    13.        
    14.         //GUILayout.EndArea();
    15.         GUILayout.EndScrollView();
    16.     }
    17. }
     
    ooxcit, Dorque, Lohaz and 1 other person like this.
  3. ClearRoseOfWar

    ClearRoseOfWar

    Joined:
    Sep 6, 2015
    Posts:
    89
    Thank you!
     
  4. dweekdev

    dweekdev

    Joined:
    Aug 2, 2021
    Posts:
    11
    If you want the scrollbar to set automatically according to items in your EditorWindow do this

    Vector2 scrollPosition = Vector2.zero;
    private void OnGUI()
    {
    scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, true);

    //Your Functions...

    GUILayout.EndScrollView();
    }