- Home /
Unity UI Issue - Fit Panel to child Text, with a Max Size
Hi there!
I'm trying to display subtitles, where the text has a panel as background that fits the size of the text. I have achieved this by adding a Horizontal Layout Group to the parent panel, as well as a content Size fitter. I tick "Child Controls Size" on the Layout Group, and use Preferred size for the horizontal fit on the Content Size Fitter. Secondly, I added a Layout Element, to the child text, and ticked "Preferred Width". By doing this I get the panel to fit nicely around the text.
My problem is that I can't find a way of limiting the size. If there is a lot of text, rather than wrapping the text, it simply expands, until it goes off screen. If I add a preferred size to the Layout Element of the child text, it simply "locks" the size to the given value, and does not resize in a flexible manner.
What I have tried: I've played around with any thinkable combination of Min Width, Preferred Width, and Flexible width values, with no usable results.
I also tried adding another panel as the parent to the existing parent (or grandparent to the text), trying to limit the size there, by using a Horizontal Layout Group, and Content Size fitter (ensuring I remove the Content Size Fitter from the original panel).
I've been searching the web, and the suggested solutions is basically some of the things I've already tried. I've considered changing the size dynamically by code, but I don't think that's a good solution.
Basically what I need is the ability to resize the parent panel dynamically, but with a restriction on the max size.
Any hints or help is appreciated - Thanks for reading!
Your answer