A grid can be seen as a container, similar to an array. Most grids are 2D, and so most grids work like 2D arrays.
Grids can contain any type of object. For instance, the following grid will contain boolean values in its cells:
The things inside grids are called cells. Normally, you need to make your own cells to suit your game.
There are two types of basic grids: uniform grids, and spliced grids.
- Uniform grids are grids where each cell has the same shape and orientation. There are four built-in grids that are uniform: RectGrid, DiamondGrid, PointyHexGrid, and FlatHexGrid.
- Spliced grids are grids made from splicing the cells of a uniform grid into new, smaller cells. In general, these cells may not have the same shape or orientation. There are 5 built-in spliced grids: PointyTriGrid, FlatTriGrid, PointyRhombGrid, FlatRhombGrid, and CairoGrid. The original grid that was spliced is called the spliced grid’s base grid.
Some grids can be wrapped, and this is how polar grids are implemented.
There are also some other grids:
- line grid (1D grid)
- nested rect grid (composite 2D grid)
- layered grid (composite 3D grid)
- generic spliced grid (2D grid)
When you work with grids in the editor, the way the grid is rendered is important. We use the following to distinguish between these different types:
- Tiled Grid: a grid where each tile is rendered as a separate object. Most simple grids are rendered with sprite tiles. Polar grids are rendered with mesh tiles (each cell has a mesh created dynamically). It’s also possible to use blocks (3D objects)
- Mesh Grid: a grid where the entire grid is rendered as a single mesh. (These grids are not supported at the moment, but you can get an idea for how they will work from this example.)
This is what we use to access the contents of a particular cell. In general, a point can be anything. The built-in grids, however, are all accessed through grid points that look like integer coordinates. The coordinates of uniform grids work like vectors: you can add and subtract them, and scale them up or down (multiply or divide by an integer).
The coordinates of spliced grids have two parts: a vector part (based on the base grid), and an index part, indicating the particular slice. We call these coordinates spliced points, and they follow a simple algebra. Below, N is the splice count, the number of cells each base cell grid has been spliced into.
[x0, y0, i0] + [x1, y1, i1] = [x0 + x1, y0 + y1, (i0 + i1) % N] (Adding two spliced points)
[x0, y0, i0] + [x1, y1] = [x0 + x1, y0 + y1, i0] (Adding a normal point to a spliced point)
A map is the thing that converts between Unity coordinates and grid coordinates. Grids themselves do not know where they are in the Unity world. They only know which cells are inside them or not, and how cells relate to each other (being neighbors, for instance). The missing information is provided by maps. Keeping this information separate is extremely useful. For instance, hex grids and brick grids are topologically equivalent, which is a fancy way of saying their cells have the same neighbor relationships. They merely differ in the shape of the cells, and hence, how world and grid points map from one to the other. This means we can use the same grid, but different maps for the two.
Maps are where the real power lies in the Grids library. They make it possible to do unusual things with grids that are not very easy with more rigid designs, including implementing pseudo irregular-grids, animating cells, transforming grids, and many more.
Maps support index syntax. If you feed it a Unity vector, it gives you back a grid point; if you feed it a grid point, it gives you a unity vector.
Maps supports several operations, including transformations, alignment functions, and anchoring functions. For a bit more detail, see Working with Maps.
Grid builders are used to build grids in the editor. Each grid has its own grid builder. Usually, it is obvious which builder to use. In some cases, you may need to consult the Grid Index. When you use a grid builder, the grid and map is configured automatically. You can access these through GridBehaviours.
Op and ShapeInfo
You may see some of these classes in the folders with grids, points and vectors, and wonder what they are. These are behind-the-scene classes that implement the shape building technology for grids. For the most part, you do not need to know what these are unless you implement your own grids with shape building technology. They are the intermediary results of shape-building expressions like these:
PointyHexGrid<int> grid = PointyHexGrid
See Constructing Grids for more details on how to use these shape building functions.