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.
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
Click Generate Grid in the inspector ensuring that relevant settings are selected
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();
}
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;
}
-
mapShape
determines the overall shape of the map, available options are- Rectangle
- Hexagon
- Parrallelogram
- Triangle
-
mapWidth
anint
, controls the number tiles wide the map is, for hexagonal shape the larger ofmapWidth
andmapHeight
is picked and used as a radius. -
mapHeight
anint
, controls the number of tiles high the map is -
hexOrientation
the orientation of the individual hexes, available options are -
hexRadius
afloat
, controls the radius of hex tile meshes, in unity units, measured from the centre of the hex to a corner, all corners are equidistant from the centre -
hexMaterial
aMaterial
, applied to the hex tile meshes, if not specified defaults to unity's diffuse material -
addColliders
abool
, when true grid generation will add a mesh collider to the tiles using the same mesh as the tiles -
drawOutlines
abool
, when true grid generation will add line renderers to the tiles and draw outlines for the meshes -
lineMaterial
aMaterial
, applied to the line renderers for drawing outlines, if you want outlines add theLines
material, included in the package, to the inspector field. Apparently I couldn't load the default Sprites/Default material via code