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;
}-
mapShapedetermines the overall shape of the map, available options are- Rectangle
- Hexagon
- Parrallelogram
- Triangle
-
mapWidthanint, controls the number tiles wide the map is, for hexagonal shape the larger ofmapWidthandmapHeightis picked and used as a radius. -
mapHeightanint, controls the number of tiles high the map is -
hexOrientationthe orientation of the individual hexes, available options are -
hexRadiusafloat, 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 -
hexMaterialaMaterial, applied to the hex tile meshes, if not specified defaults to unity's diffuse material -
addCollidersabool, when true grid generation will add a mesh collider to the tiles using the same mesh as the tiles -
drawOutlinesabool, when true grid generation will add line renderers to the tiles and draw outlines for the meshes -
lineMaterialaMaterial, applied to the line renderers for drawing outlines, if you want outlines add theLinesmaterial, included in the package, to the inspector field. Apparently I couldn't load the default Sprites/Default material via code





