When to use Grid Layout?

I’m currently building an inventory panel with fixed number of inventory slots. Each slot will eventually have a child GameObject which will be the delegate of the item placed inside.
My question is:
Since the inventory has a pre-defined and not variable number of slots, is it better to use GridLayout or simply hardcode it?
Is it feasible to use Grid Layout at all?

Grid Layout gives a lot of nice control around sizing and spacing around the grid, and between objects in the grid. So it sounds perfect for what you want to do.

I’d suggest creating a prefab of one of the cells, and that use your scripts to add and config your fixed number of cells to the grid as children of the “Content” gameobject which has the GridLayout component on it

the benefit of a grid layout is that you can just instantiate your objects at runtime and make the content object of the grid layout their parent and the positioning and sizing is done automagically.

1 Like