Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resizing a label/textfield after content.

Discussion in 'Immediate Mode GUI (IMGUI)' started by half_voxel, Apr 3, 2008.

  1. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    977
    Hi.
    I wonder if you can automatically resize a textfield/label depending on the content, i have looked in the docs but i could not find anything.
    (this is for scrolling).
     
  2. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    Assuming I understand you right, if you set the StretchWidth and StretchHeight flags in your label style (in the skin), then it will size to fit the content until it fills the available space for that element (assuming you're using GUI.Layout and not fixed GUI).

    Some more tips if you want the window background to change size, too.

    1. Set the Window or Area rect to a large value and use a style with no background texture on those elements. I usually constrain the width and let the height stretch but of course you can do it the other way or both.

    2. Contain your label element in a Vertical or Horizontal group with the background set.

    Code (csharp):
    1. GUILayout.BeginVertical("style_with_a_background");
    2. GUILayout.Label("My Text", "style_with_no_background");
    3. GUILayout.EndVertical();
    4.  
     
  3. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    977
    well.I have a label (multi row). but sometimes the text in it is larger then the actual label area so it will be clipped, now i want scrollers to appear when the text is larger then the label size. And i'm using fixed gui but i think i can replace that with GUILayout if it's needed.
     
  4. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    OK, then you need to look at a ScrollView to contain your text. Especially with GUI.Layout, they work pretty automagically.