How change expanding direction of VerticalLayoutGroup in Unity without rotating?
Asked Answered
S

3

6

I need to change the expanding direction of the VerticalLayoutGroup. With the default behavior the group will expand downstairs. What i want is, that the group will expand upstairs.

The expected behavior is described in this Video. (Link to the answer on stackoverflow https://mcmap.net/q/1738091/-how-to-change-direction-of-expanding-vertical-layout-group-unity3d)

The solution on the link is, to rotate the LayoutGroup about 180°. Now this seems more like a workaround, because all childs have to be rotated as well. The solution suggested by this one will not give the expected behavior shown in the video.

I added the ContentSizeFitter to the LayoutGroup like described in this link in the Unity Answers. Now the LayoutGroup will expand in both directions (upstairs and downstairs).

Is there any solution to accomplish the desired behavior without rotating the LayoutGroup?

Edit: I also noticed a downside from the rotating approach: The Billboard Script from the MixedRealityToolkit will force the LayoutGroup to rotate back to 0°.

Sarcocarp answered 21/3, 2019 at 11:12 Comment(0)
E
19

This works for me:

Unity Inspector screenshot

Note the Child Alignment setting on the Vertical Layout Group, and the Pivot settings on the Rect Transform. This makes the layout group expand upwards when more items are added.
Also note the presence of a Content Size Fitter component, and its Vertical Fit set to Preferred Size, that will adjust the height of the layout group when content is added or removed.

Extinguisher answered 21/3, 2019 at 12:11 Comment(3)
This didn't work for me until I added the content size fitter component, and set the Vertical Fit to "Preferred Size".Manlove
@MaxIzrin check the image! The Content Size Fitter component is there, exactly as you describe ;) But I will clarify my answer with this in mindExtinguisher
@frankhermes Sorry I wasn't clear, I meant I had to add it to the objects inside the layout as well, though Unity warns against this, it just doesn't seem to work without it.Manlove
E
1

Old question but the accepted answer makes it more complicated than necessary: the desired direction of expansion can be controlled via the sign of Spacing in the VerticalLayoutGroup.

See this GIF

Edveh answered 5/10, 2022 at 14:32 Comment(4)
An example would be nice here.Interatomic
I don't think you're right. You can see in the screenshot in the answer that it has a negative spacing. That only makes items in the layout overlap each other, and doesn't change the way the layout resizes itself, afaik.Extinguisher
Hi, @frankhermes, I added a GIF to my answer. If you make the Spacing small enough, your elements will overlap, that is right. However, changing the sign of Spacing will simply align them in the given distance into the other direction.Edveh
Altough this could be the easiest workaround in several use cases. It's required at least, elements with same height. If not, these elements will overlap each other, which is why we use vertical layout group in first place.Gavelkind
M
1

Changing the pivot in y direction will do the trick.

Mireillemireles answered 11/9, 2023 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.