I’d like to know what I can do to have some UI elements arrange automatically when they’re added under a transform in the way shown in the picture below.
I tried using the Grid Layout Group, but it does not arrange in the way that I want despite playing with all the options available. What can I do to arrange my elements like in the picture below?
I needed this functionality recently too, and solved it by copying the Grid Layout Group component from the UGUI library and adding in a hot-fix to give it the desired functionality. I’ve uploaded the fixed component to github, which you can download here: GitHub - mrbeardy/BeardyGridLayout.
This issue happens because as it wraps around, it uses the width or height of the main axis to determine how much space is required for the overflowing cells, causing the offset to be based on a width/height of more cells than there are in that row/column.
This fix checks to see if we’re on the last row/column, and use the width/height of the number of remaining cells when choosing the starting offset.
It also supports a Vertical starting axis, although you’ll need to change the Child Alignment to Middle Left/Center/Right.
Since the Grid Layout Group component has so many options, it made it difficult to come up with a solution that would work for every scenario, so it comes with a few issues when you’re trying to use certain settings, but for the traditional horizontal/vertical middle-center aligned requirements it should work. Pull requests are welcome!
Hey! Hmm, do you just mean using a fixed column/row count? Currently I have it set to flexible, but using a fixed column count gives something like this, is this what you meant?
There’s also some stuff you can do with vertical alignment, maybe that’s what you’re after.
Feel free to draw a mock-up and maybe we can figure it out.
@Ultraspider Managed to fix the issue. The issue seemed to be happening because UIExtensions was trying to deal with a GridLayoutGroup from UGUI somewhere, and since I was just inheriting from the class above that, it wasn’t working properly.
I’ve updated the code to now inherit directly from UGUI’s GridLayoutGroup, so now my version should work in any place that UGUI’s did previously. It also means that I don’t need to add in as much stuff and I was just able to override the parts I needed, so the code has been cut down a lot shorter now, which is cool. The changed code is still pretty chunky, but that’s just because Unity’s SetCellsAlongAxis method is doing so much.
Just a general comment on this package, I don’t really like this being its own package, as it’s such a small change to be in a package by itself. I might spend some time and see if I can get it added into UIExtensions, as I think that’ll be a better place for it.
@Mrbeardy I did find a bug in the package. Whenever I use Child Alignment Middle Center and Start Corner Upper Left, the bottom row that isnt full will be displayed outside the content view.