Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Using a Horizontal Layout Group to position Text and Buttons with arbitrary, mutable text

Discussion in 'UGUI & TextMesh Pro' started by Mhin-Ra, Feb 8, 2023.

  1. Mhin-Ra

    Mhin-Ra

    Joined:
    Mar 29, 2015
    Posts:
    6
    I've seen about a thousand versions of this question in the several days I've spent trying to figure out how to do what seems like roughly the simplest imaginable use case for a UI system. Unfortunately, they all end up suggesting unchecking Child Force Expand (which does not work) or using a Content Size Fitter (which is strongly discouraged by Unity UI and documentation if it's on a child of a layout group). So, here's my setup:

    Vanilla game object with three children:
    upload_2023-2-8_15-28-19.png

    The top object has a Horizontal Layout Group:
    upload_2023-2-8_15-28-36.png
    The children are completely normal Text Mesh Pro Buttons and Text Controls, which I've disabled wrapping for:
    upload_2023-2-8_15-31-59.png

    What I want is to type in (or set via script) arbitrary text, long or short, into each button and text control. I want each of those controls to resize themselves horizontally to minimally fit their text. Then I want the parent layout group to lay out the three controls from left to right, not overlapping.

    That is not what happens; instead, you get something like this:
    upload_2023-2-8_15-33-57.png
    (red text is the first button, blue text is the second text control, fushia text is the third button)

    This seems like such an obvious and simple scenario, I'm certain I'm missing some fundamental lens about how Unity UI wants me to think about this. But after reading the docs many, many times, I cannot find it. Can someone explain, step by step (because I don't know which probably obvious step I'm missing :) how I would setup these four (logical) objects to achieve this seemingly simple goal?
     

    Attached Files:

    Last edited: Feb 8, 2023
    TomGoethals and dlbcb like this.