Search Unity

Getting GUILayout.Window to automatically expand size depending on content

Discussion in 'Immediate Mode GUI (IMGUI)' started by Pi_3-14, Jan 1, 2014.

  1. Pi_3-14

    Pi_3-14

    Joined:
    May 9, 2012
    Posts:
    168
    I'm not even sure if it is possible to do this.

    I'm creating a GUILayout.Window that contains a single GUILayout.Label.

    I'm trying to get the window to wrap around the label, so to speak.

    Here is my code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Fail : MonoBehaviour
    6. {
    7.     Rect R;
    8.  
    9.     void Awake( )
    10.     {
    11.         // as per http://forum.unity3d.com/threads/59048-GUI-Window-problems
    12.         //  start by positioning window offscreen, size = (1,1)
    13.         // the first repaint event will adjust this rectangle
    14.         R = new Rect( Screen.width+1, Screen.height+1, 1, 1 );
    15.     }
    16.  
    17.     bool first = true;
    18.  
    19.     void OnGUI( )
    20.     {
    21.         Rect R_out = GUILayout.Window(
    22.                                      1
    23.                                      , R
    24.                                      , WindowFunc
    25.                                      , "Test Window"
    26.                                      , GUILayout.ExpandWidth( true )
    27.                                      );
    28.  
    29.         Debug.Log(
    30.                   Event.current.type.ToString( )
    31.                   + ",   " + R.ToString( )
    32.                   + ",   " + R_out.ToString( )
    33.                   );
    34.  
    35.         if(  first  ( Event.current.type == EventType.Repaint ) ) {
    36.             R.x = 100;
    37.             R.y = 50;
    38.             R.width = R_out.width;
    39.             R.height = R_out.height;
    40.  
    41.             first = false;
    42.         }
    43.     }
    44.  
    45.     void WindowFunc( int id )
    46.     {
    47.         GUILayout.Label( "Foo 1 2 3 4 5 6 7" );
    48.     }
    49. }
    50.  
    And here is the output:

    $Screenshot 2014-01-01 17.15.22.png

    It should be drawing the label on a single line, rather than splitting it over several lines.

    Of course, that code looks conceptually wrong. Conceptually it would make sense for EventType.Layout to calculate the rectangle, instead of EventType.Repaint. But changing it makes no difference.

    Also GUILayout.Label( "Foo 1 2 3 4 5 6 7", GUILayout.ExpandWidth( true ) ); makes no difference.

    Can anyone get this to work?

    π

    EDIT:
    If I do this:
    Code (csharp):
    1.  
    2.             R.width = 150; // R_out.width;
    3.             R.height = 80; // R_out.height;
    4.  
    ... it DOES work.

    PS Link: http://forum.unity3d.com/threads/95...Layout-MaxHeight-and-ScrollView-does-not-work
     
    Last edited: Jan 1, 2014
  2. Pi_3-14

    Pi_3-14

    Joined:
    May 9, 2012
    Posts:
    168
    I just found a good online book; http://3dgep.com/?p=5169, which states:

    So I tested using:
    Code (csharp):
    1.  
    2.         Rect R_out = GUILayout.Window(
    3.                                      1
    4.                                      , R
    5.                                      , WindowFunc
    6.                                      , "Test Window"
    7.                                      , GUILayout.Width( 200 )
    8.                                       );
    9.  
    And this does correctly adjust the vertical height:

    However I can't see any way to get it to adjust to the correct width.

    $Screenshot 2014-01-01 20.55.00.png