Search Unity

Editor Extension + Lots of GUI Buttons

Discussion in 'Editor & General Support' started by Shane-C, Apr 24, 2015.

  1. Shane-C

    Shane-C

    Joined:
    Nov 9, 2012
    Posts:
    211
    I am developing an Editor Extension that utilizes a grid of buttons. This grid can be extremely large at times, like 50 x 1000 buttons. I have wrapped the grid into a scrolling container, so only a portion of the grid is shown at any given time, but I'm trying to figure out a way to prevent the unseen buttons from drawing entirely.

    Here is a sample of what I'm trying to do:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. public class SampleSlowdown : EditorWindow
    6. {
    7.     private Vector2 _scrollPosition = Vector2.zero;
    8.     private int _rowCt = 10;
    9.     private int _newRowCt = 10;
    10.     private int _colCt = 10;
    11.     private int _newColCt = 10;
    12.  
    13.     // Add menu item named "GoogleFu" to the Window menu
    14.     [MenuItem("Window/SampleSlowdown")]
    15.     public static void ShowWindow()
    16.     {
    17.         //Show existing window instance. If one doesn't exist, make one.
    18.         EditorWindow sampleWindow = GetWindow(typeof(SampleSlowdown));
    19.         sampleWindow.title = "Sample Window";
    20.     }
    21.  
    22.     void OnGUI ()
    23.     {
    24.         _scrollPosition = EditorGUILayout.BeginScrollView (_scrollPosition);
    25.  
    26.         for (int row = 0; row < _rowCt; ++row) {
    27.  
    28.             EditorGUILayout.BeginHorizontal();
    29.             for(int col = 0; col < _colCt; ++col) {
    30.                 GUILayout.Button( row + " - " + col, GUILayout.Width(80), GUILayout.Height(25));
    31.             }
    32.             EditorGUILayout.EndHorizontal();
    33.         }
    34.         EditorGUILayout.EndScrollView ();
    35.  
    36.         EditorGUILayout.BeginHorizontal ();
    37.         EditorGUILayout.LabelField ("Rows: ");
    38.         _newRowCt = EditorGUILayout.IntField (_newRowCt);
    39.         EditorGUILayout.EndHorizontal ();
    40.  
    41.         EditorGUILayout.BeginHorizontal ();
    42.         EditorGUILayout.LabelField ("Cols: ");
    43.         _newColCt = EditorGUILayout.IntField (_newColCt);
    44.         EditorGUILayout.EndHorizontal ();
    45.  
    46.         if (Event.current.type == EventType.Repaint) {
    47.             _rowCt = _newRowCt;
    48.             _colCt = _newColCt;
    49.         }
    50.     }
    51. }
    Simply copy this script into an Editor folder and run it by opening the Window->SampleWindow
    You can see that small grids, like the default 10x10 works just fine.
    If you change the rows to something like 1000, the editor window becomes very unresponsive.