Quick-Start Tutorial

This tutorial shows you how to set up a hexagonal grid for a 2D game. I assume you already have a tile object defined that you want to use in your grid, and that is of type SpriteCell. Grids come with a few built-in cell prefabs, but those are intended for our examples. In general, you would make your own cells for your specific game.
1Start with the main class which will build the grid, and handle inputs to the grid. Here is the shell for that class, plus the declarations we will need:

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

usingUnityEngine;

usingGamelogic;//Include this to use GLMonoBehaviour

usingGamelogic.Grids;//Include this to use Grids

publicclassPointyHexTestDoc:GLMonoBehaviour

//(GLMonoBehaviour has some extra convenience functions, such as a generic Instantiate)

{

publicSpriteCell cellPrefab;//The is the prefab that will be used for cells.

publicGameObject root;//This is the root object for all cells

//A grid of pointy hex cells that take SpriteCells as objects

privatePointyHexGrid<SpriteCell>grid;

//A map that can convert PointyHexPoints to Vector3

privateIMap3D<PointyHexPoint>map;

publicvoidStart()

{

BuildGrid();

}

privatevoidBuildGrid()

{

// ...

}

publicvoidUpdate()

{

// ...

}

}

2In the BuildGrid method, define the grid structure. This is the data structure that will contain the cells, and it works very similar to a 2D array. The simplest way to do this is to use one of the standard shape functions provided for each grid type. These functions determine the shape of the grid (not the shape of the cells!) For this tutorial, we will use a rectangular shape. (Fat rectangles have one cell extra in every second row. Thin rectangles have one cell less in every second row. Ragged rectangles have the same number of cells in each row.)

C#

1

2

3

4

constintwidth=3;

constintheight=5;

grid=PointyHexGrid<SpriteCell>.FatRectangle(width,height);

(There are two more advanced ways to create different shapes if the standard shapes do not meet your needs. See Constructing Grids for more information).
3Next, we need to define a map. A map determines where the grid is located in space. Maps also makes it possible to align grids in a rectangle. The following will make a map that aligns our grid to the center of a rectangle, which we conveniently choose to coincide with the edges of the screen:

C#

1

2

3

4

5

map=newPointyHexMap(cellPrefab.Dimensions)

.WithWindow(ExampleUtils.ScreenRect)

.AlignMiddleCenter(grid)

.To3DXY();//Converts 2D map to a 3D map.

}

There are more commands that you can chain in map creation expressions that give you more low-level control over the positioning and rotation of a grid. For more information on how maps work, see Working with Maps.

4The next step is to instantiate our cells, and put them in the right positions using our map. We also assign them to corresponding positions in the grid data structure:

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

foreach(PointyHexPoint point ingrid)

{

SpriteCell cell=Instantiate(cellPrefab);

Vector3 worldPoint=map[point];

cell.transform.parent=root.transform;

cell.transform.localScale=Vector3.one;

cell.transform.localPosition=worldPoint;

//Depending on your game, you will have a different

//cell that will have different methods.

cell.Color=ExampleUtils.Colors[point.GetColor3_7()];

cell.name=point.ToString();//This is used by gizmos in the editor

grid[point]=cell;

}

Note that we use points to access the elements of a grid. Each grid uses its own point type. A grid point works similar to a 2D Vector, except that coordinates are always integers. Most point types provide static constants that correspond to the cardinal directions of the grid. For instance, RectPoint provides North, East, South, West. PointyHexPoint provides NorthEast, NorthWest, West, SouthWest, SouthEast, and East.
5To make our cells clickable, we need to process mouse inputs, typically in the Update function