I am having trouble controlling the size of elements added to a vertical layout group. If I add a button to a gameobject that has a vertical layout group, the button says ‘Some values driven by vertical layout group’. I cannot control the width and height. But the only inputs the vertical layout group has are padding, alignment, force width and height. The force width and height maximize it there isn’t a min, max or desired like on the grid layout group. How do I control the button size? Thanks.
If you want to control the width and height of a child element of a layout group, then simply add a LayoutElement and set the Min / Preferred or flexible values for each axis.
You will also have to uncheck the “Force child expand” option on the group itself
So I would add that element to each (or the first one before I copy)? The problem I was having is the elements would stretch to fill the parent. I wanted to be able to add any number of buttons and have them space the same. I was able to achieve that with the grid, but it seemed like I should be able to with the Vertical Layout.
In either case, how can I automatically size the height of the parent panel to match the number of elements? Thank you for your time.
You add it to each child (not the group)
So
-
Group (Vertical Layout Group)
-
Child 1 (control + layout element)
-
Child 2 (control + layout element)
-
etc
If you want to you can prefab the child to avoid recreating it every time.
If you want the panel to only be as big as it’s children, then also add a Content Size Fitter to the group, e.g.
-
Group (Vertical Layout Group + Content Size Fitter)
-
Child 1 (control + layout element)
-
Child 2 (control + layout element)
-
etc
This will size the panel to how many children you have instead of the default which is the other way round (sizing children to fit the panel)
This was the answer I was looking for! Make this more findable ![]()
Awesome, great help, thanks!