- Home /
[4.6 GUI] Resizing panel to fit it content
So my menu is almost done. It is made to dynamically create list of items from a specific array and uses VerticalLayoutGroup to make it fit together. So far, so good. But there is one small issue, I don't know how to resize panel those buttons used for menu reside in to fit it content which results in menus scrollable too far away in case of short menus or menus that have too short scrollable area to see all items (long menus).
If I use RectTransform.rect.height to try to set height, it says it's read only.
If I try to use sizeDelta, it kinda works, but there is still an unwanted buffer (scrolling past the menu).
Answer by PorkMuncher · Jan 25, 2015 at 08:28 PM
Try "Content Size Fitter" component!
Thank you so much! Need to get used to this, I'm too used to old GUI and so I was trying to do most of it in code.
Answer by anisabboud · Jul 15, 2015 at 04:59 PM
Add "Content Size Fitter" component to the panel, and set "Vertical Fit" to "Preferred Size".
Add "Vertical Layout Group" component to the panel, and set the padding+spacing+alignment as you please.
For each child, add a "Layout Element" component, and set the "Preferred Height" as you please.
Your answer
Follow this Question
Related Questions
My canvas recttransform.rect.height don't change properly 1 Answer
[4.6 gui] Getting dimensions of 2D UI object (button for example) 2 Answers
Scaling image width in script error 0 Answers
How to set RectTransform size relative to the size of the textbox? 1 Answer
How to find width and height of game object, Unity2D 2 Answers