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

GUILayout.Window with GUILayout.MaxHeight and ScrollView does not work

Discussion in 'Immediate Mode GUI (IMGUI)' started by Gustav, Jul 1, 2011.

  1. Gustav

    Gustav

    Guest

    Hi,

    I'm trying to make a GUILayout.Window with a maximum and a minimum height. If the the content takes more space than the maximum height of the window, a scrollbar should appear, if not, the height of the window should be as high as the content an no scrollbar should be visible. I tried for hours, but I didn't get it working :-(

    It seems that GUILayout.MaxHeight doesn't work correctly.

    Unity 3D Pro 3.3 on Windows 7

    I found out the following things:
    • If I don't use GUILayout.BeginnScrollView the GUILayout.Window resizes its height to the size of the content if the windowRect.height is SMALLER than the content.
    • If I activate the scrollview, the window is only as heigh as the windowRect.height although the content is higher (= strange)
    • If I use scrollview the windowRect.height has to be higher than the MaxHeight-Value to resize the window to the maxheight value (that makes sense)
    • If the content is smaller than the maxheight-value and scrollview is used, the window is always as high as the maxheight-value and does not resize to the height of the content (that makes absolutly no sense)

    If I activate the lines Button 4 - 19 the code does what it should do: scrollbar appears, windows' height is 200

    If I activate only the Buttons 1 - 3 the window's height is also 200 but it should only be as high as the three buttons.

    Here is the code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class example : MonoBehaviour
    6. {
    7.     private Rect windowRect = new Rect(10, 10, 200, 700);
    8.     private Vector2 scrollpos;
    9.  
    10.     void OnGUI()
    11.     {
    12.         windowRect = GUILayout.Window(0, windowRect, myWindow, "Testwindow", GUILayout.MaxHeight(200));
    13.     }
    14.  
    15.     void myWindow(int windowID)
    16.     {
    17.         scrollpos = GUILayout.BeginScrollView(scrollpos);
    18.  
    19.         GUILayout.Button("Button 1");
    20.         GUILayout.Button("Button 2");
    21.         GUILayout.Button("Button 3");
    22.  
    23.         /*        
    24.         GUILayout.Button("Button 4");
    25.         GUILayout.Button("Button 5");
    26.         GUILayout.Button("Button 6");        
    27.         GUILayout.Button("Button 7");
    28.         GUILayout.Button("Button 8");
    29.         GUILayout.Button("Button 9");        
    30.         GUILayout.Button("Button 10");
    31.         GUILayout.Button("Button 11");
    32.         GUILayout.Button("Button 12");
    33.         GUILayout.Button("Button 13");
    34.         GUILayout.Button("Button 14");
    35.         GUILayout.Button("Button 15");
    36.         GUILayout.Button("Button 16");
    37.         GUILayout.Button("Button 17");
    38.         GUILayout.Button("Button 18");
    39.         GUILayout.Button("Button 19");
    40.         */
    41.  
    42.         GUILayout.EndScrollView();        
    43.     }
    44. }
    45.  
    Hope somebody can help me! THX in advance!
     
  2. Gustav

    Gustav

    Guest

    Any ideas?
     
  3. DuckyCrayfish

    DuckyCrayfish

    Joined:
    Nov 27, 2012
    Posts:
    1
  4. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    I think you are expecting to much from GUILayout :) This is not a multi-pass layout system which could handle children size changes. You just have to size a bunch of stuff manually.
     
  5. Pi_3-14

    Pi_3-14

    Joined:
    May 9, 2012
    Posts:
    168
    Last edited: Jan 1, 2014
  6. Komak57

    Komak57

    Joined:
    Apr 25, 2013
    Posts:
    4
    This seems to occur when a GUILayout.Window contains variant quantities of items during various repaints. In my project (4.5) I created a sort of tool-tip window that could display 2, or 4 elements (will be more) depending on the object hovered over. Now, when swapping from tab to tab, I only got one ArgumentException on the first attempt (cycling from 0 to 4). when swapping to an Item, it cycles down from 4 to 2, and has no complaints. When cycling back up to 4, it causes 2 additional ArgumentException for item 2 and 3 (0 based, so the 3rd and 4th new items). Still working towards finding a safe time to repaint to bypass the error altogether, but generating 2 empty labels helped deal with all but the first.