[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).

Try “Content Size Fitter” component!

  1. Add “Content Size Fitter” component to the panel, and set “Vertical Fit” to “Preferred Size”.
  2. Add “Vertical Layout Group” component to the panel, and set the padding+spacing+alignment as you please.
  3. For each child, add a “Layout Element” component, and set the “Preferred Height” as you please.