- Home /
How to make UI Panel scale to fit content
Im trying to learn the new UI system.
Heres a simple panel with text as its child (note, the pointer on the topleft is just an image and is not relevent)
I'd like the panel to resize based on the text within in it something like this.
Ive tried adding a layout element to the text and a horizontal layout group and content size fitter to the panel (and a bunch of other combinations) but I always get really weird behaviour (for example the panel growing exponentially bigger than the text).
I'm on the cusp of just getting getting the behaviour I want through code, but I want to give the new UI system a go.
This is a pretty good explanation of how to do it. I didn't find the Unity documentation especially helpful on this subject.
Answer by deltron1830 · Mar 26, 2016 at 05:17 PM
So you need to put a content size fitter and also a layout group (horizontal or vertical, doesnt matter as there is only one child) on the Panel.
But a really important thing that I missed is that you must correctly set the pivot and anchor of the panel and text before you add those components. Any changes you try to make to them after adding those components will be ignored and their positions definitely affects how the text will expand. Also if your text is scaled you will get weird behaviour (ie. the panel growing exponentially bigger than the text)
Answer by Happy-Zomby · Mar 25, 2016 at 07:09 PM
Hi, did you have a look here? http://docs.unity3d.com/Manual/HOWTO-UIFitContentSize.html
I guess you could make the image a child of the text - and then put a "size fitter" component on that text box.
hope that helps,
Your answer
![](https://koobas.hobune.stream/wayback/20220612073047im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using iTween with the new Unity UI 2 Answers
Fill empty space in parent in UI Layout 1 Answer
Cannot get text+image always centered 1 Answer
UI: Creating UI based on many strings 0 Answers
Make UI always center the panel 1 Answer