Search Unity

  1. Unity 2018.3 is now released.
    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. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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

Discussion in 'Extensions & OnGUI' started by keenanwoodall, Nov 10, 2018.

  1. keenanwoodall

    keenanwoodall

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