Home

Awesome

UnityHexGrid

A hex grid generation tool, for use in unity. Much of the hex logic is drawn from Red Blob Games. I made this because I initially struggled with the implementation so now you don't have to. Feel free to leave any feedback or suggestions, I will consider implementing suggestions that I think are generally useful to all users.

Installation

Download unity package from releases

In unity Assets > Import Package > Custom Package

Navigate to downloaded file and select it

Create an empty gameObject ctrl+shift+n and add Grid script to it

Generating a Grid in edit mode

Click Generate Grid in the inspector ensuring that relevant settings are selected

Generating a Grid at runtime

Ensure relevant settings are set and call GenerateGrid() on an instance of Grid

public Material hexMaterial; //Assigned in inspector
public Material lineMaterial; //Assigned in inspector

private Grid grid;

private void Start() {
	//Set grid settings
	grid.mapShape = MapShape.Rectangle;
	grid.mapWidth = 5;
	grid.mapHeight = 5;
	grid.hexOrientation = HexOrientation.Flat;
	grid.hexRadius = 1;
	grid.hexMaterial = hexMaterial;
	grid.addColliders = true;
	grid.drawOutlines = true;
	grid.lineMaterial = lineMaterial;

	//Gen Grid
	grid.GenerateGrid();
}

Access tiles at runtime

Call Tiles on an instance of Grid, returns a Dictionary<string, Tile> where the string is constructed from the tile's coordinates.

private Grid grid;

private void Start() {
	var tiles = grid.Tiles;
}

Grid Settings