Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Make size of UI.Text fit to content without UI.ContentSizeFitter

Discussion in 'UGUI & TextMesh Pro' started by BrightBit, Nov 18, 2015.

  1. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    I want some kind of auto size functionality for the UI.Text element. The ContentSizeFitter seems to do exactly what I am looking except that it won't work if the Text itself is part of a hierarchy where a parent element already contains the ContentSizeFitter component.

    The manual writes something about how to "Make children of a Layout Group fit their respective sizes" but even this approach doesn't work for me.



    Am I doing something wrong? Do I have to write a custom script that does what I am looking for?
     
  2. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    Noone knows an answer?
     
  3. Stephan-B

    Stephan-B

    Unity Technologies

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Are you trying to have the orange background element automatically re-size to fit the text?

    What Layout components do you have on the parents of the text object? Do you have a ContentSizeFitter as well as a VerticalLayoutGroup or HorizontalLayoutGroup?
     
  4. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    Yes, I am trying to make the orange background element automatically resize to also contain the text.

    This is the hierarchy:

    Window (with Image, ContentSizeFitter and VerticalLayoutGroup)
    InnerArea (with Image and LayoutElement [that ignores Layout])
    ClientArea (with Image, VerticalLayoutGroup and LayoutElement)
    Text (with Text and LayoutElement)
    Choice 0 (with Button and LayoutElement)
    Choice 1 (with Button and LayoutElement)
    Choice 2 (with Button and LayoutElement)
    Choice 3 (with Button and LayoutElement)​

    I've attached a small test scene wiht this hierarchy so you can see it for yourself if you want.

    Edit: I'm using Unity 5.2.0f3 Personal
     

    Attached Files:

  5. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    *sigh* So it's not possible without a custom script, right?
     
  6. Chichkan

    Chichkan

    Joined:
    Jun 15, 2015
    Posts:
    5
    You have to set "Preffered Size" in Vertical Fit in Content Size Fitter of "Window" instead of "Min Size".
     
    BrightBit likes this.
  7. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    @Chichkan: You sir rock! :D Thank you so much for pointing that out. It works flawlessly.