Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

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

Discussion in 'Extensions & OnGUI' started by keenanwoodall, Nov 10, 2018 at 6:32 AM.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    535
    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 :)