Search Unity

Layout Group content equal size

Discussion in 'UGUI & TextMesh Pro' started by Julien_at_work, Oct 12, 2018.

  1. Julien_at_work

    Julien_at_work

    Joined:
    Aug 9, 2018
    Posts:
    35
    Hi,
    What i have:
    Vertical Layout Group
    --Horizontal Layout Group
    ----TMP Text x2
    --Horizontal Layout Group
    ----TMP Text x2
    --Horizontal Layout Group
    ----TMP Text x2
    All the TMP Texts have autosize toggled on.
    My problem is, that i can't find a combination of settings that equalizes the heights of the horizontal layout groups inside the parent vertical group. Some combinations seem to almost produce the desired result, but the horizontal groups still differ by some small amount which makes the autosize text vary slightly between horizontal groups in turn though.

    What i want:
    The horizontal groups should all have equal sizes, auto expanding with the parent vertical group. The TMP-texts should then autosize to fill each horizontal group.

    The problem seems to come from when i add the horizontal groups which grow larger with their text content. A single TMP text works fine and doesn't affect its parent

    How do i achieve that?
     
    Last edited: Oct 12, 2018
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    You might try adding a LayoutElement component to each horizontal group to override its preferred height, to ensure the preference is the same on all of them. (And presumably you'll need to make sure "force child expand" is checked in the vertical layout group.)

    Alternately, if there will always be 3 horizontal layout groups, and you always want them to have an equal share of the space, then you don't need to use a vertical layout group at all! Just set the horizontal groups' Y anchors to be 0 - .33, .33 - .67, and .67 - 1 and their top/bottom to all be 0 relative to the anchors, and they'll automatically stretch with the parent.
     
  3. Julien_at_work

    Julien_at_work

    Joined:
    Aug 9, 2018
    Posts:
    35
    Thanks, the second option was kind of what i was looking for. It's not dynamic and a lot more work the more children there are, but it works in my specific case.
     
    KuroSalamander likes this.
  4. UnmitigatedDisaster

    UnmitigatedDisaster

    Joined:
    Nov 19, 2022
    Posts:
    1
    Since this was never answered...

    For anyone out there looking for this now, the solution almost always seems to be to nest text (or sometimes even other elements like images) inside a layout element wrapper parent object. In other words, make all of the children of a layout group objects that only have the layout element component. Then if you want something like text to exist wholly within that child, make the TMP object a child that stretches within the layout element parent.
     
    Jushik, Jilbarkus and Kinnith7 like this.
  5. Kinnith7

    Kinnith7

    Joined:
    Jul 4, 2017
    Posts:
    84