Unity C# classes for constructing and using hexagonal grid coordinate systems.
Branch: master
Clone or download
grantmoore3d Merge pull request #3 from grantmoore3d/cleanup-and-example
Added example code and updated README
Latest commit 15fa5b2 Jan 17, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Assets Added example code and updated README Jan 16, 2019
images Added example code and updated README Jan 16, 2019
.gitignore Initial port of existing code, non-functional Jan 15, 2019
LICENSE Initial commit Jan 15, 2019
README.md Added example code and updated README Jan 16, 2019

README.md

unity-hexagonal-grids

Collection of cube coordinate functionality for constructing and interacting with hexagonal grids in Unity.

Constructed Grid A* Path Tracing Reachable Coordinates

Scripts

The repository contains the following scripts within the Assets folder:

Coordinate.cs

Responsible for the information pertaining to a single Coordinate within the CubeCoordinate grid; cube coordinate, transform position, A* path costs, meshes, display/hide functionality, etc...

CubeCoordinates.cs

Responsible for the construction of a hexagonal grid of Coordinate instances, managing named containers of Coordinate instances for building interactions with the grid, and performing validated coordinate manipulations.

HexMeshCreator.cs

Singleton used to provide each Coordinate with an appropriate hexagonal mesh for display.


Example

The repository contains the following example within the Assets\Example folder:

ExampleScene.unity

The scene contains GameObject named "ExampleCubeCoordinate" within the scene that has the CubeCoordinate and ExampleScript components added to it.

ExampleScript.cs

Sets up an example scene (as seen in the above images) which constructs a grid with some minimal randomization, performs a some of the coordinate manipulations and stores them in coordinate containers to be displayed using the following key presses:

  • Return > Constructs the grid
  • Backspace > Displays all grid coordinates
  • L > Displays a line between two coordinates (non-contiguous if coordinates are missing)
  • P > Displays an A* path between two coordinates
  • S > Displays a spiral, 3 coordinates away from zero
  • R > Displays a reachable set of tiles up to 3 coordinates away from zero

Example usage:

// Construct a grid with radius of 10
CubeCoordinates cubeCoordinates;
cubeCoordinates = gameObject.GetComponent<CubeCoordinates>();
cubeCoordinates.Construct(10);

// Randomly sample coordinates and add them to a "random" coordinate container
foreach(Vector3 cube in cubeCoordinates.GetCubesFromContainer("all"))
    if (Random.Range(0.0f, 100.0f) < 25.0f)
        cubeCoordinates.AddCubeToContainer(cube, "random");

// Remove "random" coordinates from the grid and cleanup
cubeCoordinates.RemoveCubes(cubeCoordinates.GetCubesFromContainer("random"));
cubeCoordinates.CleanEmptyCoordinateContainers();

//Find and display a path between two coordinates
List<Vector3> allCubeCoordinates = cubeCoordinates.GetCubesFromContainer("all");

cubeCoordinates.AddCubesToContainer(
    cubeCoordinates.GetPathBetweenTwoCubes(
        allCubeCoordinates[0],
        allCubeCoordinates[allCubeCoordinates.Count - 1],
        "path"
    )
);

cubeCoordinates.HideCoordinatesInContainer("all");
cubeCoordinates.ShowCoordinatesInContainer("path");

Developed by following this guide: https://www.redblobgames.com/grids/hexagons/