I’m totally new to Unity and probably doing something wrong. I’m trying to create a list to scroll through with a vertical layout group. The thing is, that when I create a new child for the group, the children are positioned in the wrong place. Instead of adding children to the bottom of the list, it’s repositioning all the items in the list so they’re centered at the top of the container.
My container (which is a panel) has no specific components. The content of the container is anchored to the top left.
My grid for the list (which is an image) has a “Content size fitter” and “Vertical layout group”. For the fitter I’ve set the horizontal fit to “Unconstrained” and the vertical fit to “Preferred size”. In the group I’ve set “Child alignment” to “Upper left” and I unchecked both options for “Child force expand”. Again it’s anchored to the top left.
The buttons within the list have a “Layout element” component. I’ve checked “Preferred height” and given the original height and I’ve checked “Flexible width” to make it fit the width of the panel.
Now I’m thinking that the alignment of the children should change when I change the “Child alignment” property, but sadly changing it does nothing.
Am I doing something wrong or did I stumble on a bug?
Apparently this was caused by a wrong value for the pivoting on the grid (the image I used). The default values were X = 0.5 and Y = 0.5, which I’ve changed to X = 0 and Y = 1.0 to fix the problem.
When you are using VerticalLayoutGroup or HorizontalLayoutGroup(inherited from HorizontalOrVerticalLayoutGroup) on a GameObject, you must check the recttransform.sizedelta (its width or height on Inspector). Because the calculation of the childs’ positions depend on it. Once the width and height of a Gameobject is 0, the ChildAlignment of LayoutGroup is useless because the rect of Gameobject is shrinking to one point(0,0). And contentsizefitter will set the width and height of a recttransform to zero. So be careful to use the contentsizefitter. It’s worthy spending a little time to read the source code of UGUI when you are struggling with these wired problem