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

Buttons in scroll-view won't match the window width

Discussion in 'Immediate Mode GUI (IMGUI)' started by keenanwoodall, Nov 10, 2018.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    597
    I've created a simple window and want a vertical list of buttons. I need the list to be in a scroll view in case the window isn't tall enough to show all the buttons.

    Ideally, the buttons will expand to fit the windows width unless the window is really thin. I've been messing around with GUILayout.MinWidth and GUILayout.ExpandWidth but haven't had any luck.

    (sorry for the tall gif)

    2018-11-10_00-28-51.gif

    Here's the code. It's pretty straightforward:

    Code (CSharp):
    1. private void OnGUI ()
    2. {
    3.     scrollPosition = EditorGUILayout.BeginScrollView (scrollPosition);
    4.  
    5.     EditorGUILayout.Space ();
    6.  
    7.     EditorGUI.BeginChangeCheck ();
    8.     var newAutoAdd = EditorGUILayout.ToggleLeft (autoAddContent, autoAdd);
    9.     if (EditorGUI.EndChangeCheck ())
    10.     {
    11.         Undo.RecordObject (this, "Changed Auto Add");
    12.         autoAdd = newAutoAdd;
    13.     }
    14.  
    15.     EditorGUILayout.Space ();
    16.  
    17.     if (GUILayout.Button (addDeformableContent))
    18.     {
    19.         AddDeformable ();
    20.     }
    21.  
    22.     EditorGUILayout.Space ();
    23.  
    24.     if (allDeformerAttributes == null)
    25.         EditorGUILayout.LabelField ("No deformers found with a Deformer attribute.");
    26.     else
    27.     {
    28.         foreach (var attributeInstance in nonUtilityDeformerAttributes)
    29.         {
    30.             if (GUILayout.Button (attributeInstance.Name))
    31.                 CreateDeformerFromAttribute (attributeInstance, autoAdd);
    32.         }
    33.         EditorGUILayout.LabelField (utilityContent);
    34.         foreach (var attributeInstance in utilityDeformerAttributes)
    35.         {
    36.             if (GUILayout.Button (attributeInstance.Name))
    37.                 CreateDeformerFromAttribute (attributeInstance, autoAdd);
    38.         }
    39.     }
    40.     EditorGUILayout.EndScrollView ();
    41.  
    42.     Repaint ();
    43. }
    [edit] Here's the full file if you think its relevant :)