Grids is a big library, and it may not be obvious how to start, or where to look when you know what you want to do. Here are some basic pointers.
Grids can be built in the editor, using one of the grid builders, or can be directly built in code.
You will usually want to use the first method – it’s much simpler, and the visual feedback in the editor makes it easy to see what is going on. Building grids in code is useful when you want more control and want to use advanced techniques. Grids built in code is also useful for certain types of algorithms, where the “cells” are not Unity objects.
Start by selecting the appropriate grid builder, and build a grid using the appropriate example prefab. Once you have your grid setup in the editor, the next step is to give add the logic you will need for your game. There are usually two parts:
- Making a custom cell that will keep minimal state and handle its display based on the state.
- Adding a
GridBehaviour
to handle grid logic and interaction with the grid.GridBehaviour
s are components that gives you access to the grid data and properties.
Cells are usually quite lightweight. The bulk of the logic will be in your GridBehaviour
.
The GridBevahiour
gives you access to the two main objects you will work with: the grid and the map:
- The grid is a data structure that contains your cells.
- The map “translates” grid points to Unity world points. If you want to draw a line between the centers of two cells, this is what you will use.
You can usually implement the logic in your GridBehaviour
in four steps:
- Additional initialization.
- External interaction (such as player input, or interaction with the “agents” in your game which are not “in” the grid).
- Automatic behaviour
- State checks
For example, the game Lights Out may be implemented as follows:
- Initialize a problem pattern
- Handle mouse input for toggling cells
- (There is no automatic behaviour, so we can skip this step)
- Check whether the puzzle is solved, and run the necessary code.
Similarly, a match-3 game with falling blocks may be implemented as follows:
- Initialize the game with random blocks
- Handle mouse input for matching patterns
- Run the falling blocks code
- Check whether a move is possible
Of course, game development is not usually as neat as this. In general, you may implement the four steps very crudely, and then move on to refine them.
You can find more detailed information in various articles: