How to have grid layout components with different cell heights in Unity
Solution 1
For grid layout you cannot control the cell sizes dynamically . Instead you can use horizantal / vertical layout groups where you can find an option control child size . And as mentioned in above ans add Layout element to child component and adjust the sizes
Solution 2
Both @Programmer and @Salma572 answers are true. If you want that size to be a fixed number or pixels, use a LayoutElement
. However, It doesn't seem you can use a scale (in percent) or anything else dynamic.
It's a shame but you'll probably need to write a script.
Rookatu
Updated on June 13, 2022Comments
-
Rookatu almost 2 years
I am trying to build a scene in my 2D game which has a panel whose elements are arranged as in this image:
The
ScrollRect
should dynamically add rows after users provide values to theInputField
, select an option from theDropdown
, and then click theButton
. This all works fine, except that the viewable area of for theScrollRect
has the same vertical size as its siblings within the parent panel.This happens because the
GridLayoutGroup
of the parent panel requires the child cell sizes to be specified, and if I specify a value which makes sense for the top two panels it is too short for theScrollRect
. Putting aContentSizeFitter
on the parent panel does not help (also, despite Unity warning against it, there is aContentSizeFitter
on the content of theScrollRect
since without one the newly added rows will not appear when scrolling).So my question is:
what do I need to do to allow the vertical sizing of the children of the top level panel to be based on their sizes, when one child's vertical size will change dynamically?
Thanks!