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 Making UI elements fit the size of their content without extra layout groups

Discussion in 'UGUI & TextMesh Pro' started by uwdlg, Oct 18, 2023.

  1. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    154
    Hi,
    I once again encountered the old Content Size Fitter warning about how children of layout groups shouldn't have that component and - also once again - tried to find the "correct" way. That quest lead me to this documentation page I stole part of the thread title from.
    Since I'm currently trying to have a ScrollRect's content transform fit its child buttons, I focused on this part:
    While this does produce the correct result, it seems to go against the performance tip
    from here.

    So my question is:
    Is there a way to get buttons to fit their Text child's width without amassing layout groups?

    Is it just writing a script implementing ILayoutSelfController that caches the problematic GetComponent call(s) to the child Text (or force the reference to be set in the editor) and sets the width or is there a no-code solution? I am already doing that on the SrollRect GameObject to make its width match the content width in case that influences things.
     
    Last edited: Oct 18, 2023