Making your own grid shapes

CustomEditorShapes
To make your own grid shape:

  1. Extend a new class from CustomGridBuilder.
  2. Override the MakeGrid method, and return the grid of the required shape from this method.
  3. Attach this component to the same game object as the builder you are using, and set the shape to Custom on the grid builder in the inspector.

You can use all the ways of creating new shapes in code available:

  • Static functions
  • Shape builder functions
  • Custom containment function

See Constructing Grids for more information on how to build grids in code.

It’s a good idea to test that the point type is of the right type, and print an error message if the test fails. This will prevent someone from trying to use your grid builder with the wrong type of grid.

Also note the cast before returning the grid. The cast is necessary because the point type of the grid created in code is more specific than the one required, and C# cannot do the conversion automatically.

Here is an example that makes a hexagon ring:

public class RingGridBuilder : CustomGridBuilder
{
	public int size;

	public override IGrid MakeGrid()
	{
		if (typeof(TPoint) != typeof(PointyHexPoint))
		{
			Debug.LogError("You can only use this GridBuilder with pointy hex grids", this);
		}

		return (IGrid)PointyHexGrid
		   .BeginShape()
			  .Hexagon(size)
			  .Translate(size - 2, size - 2)
			  .Difference()
			  .Hexagon(size - 1)
		   .EndShape();
	}
}

Here is an example that shows how to set it up:

(Import Grids, import the package, and open the CustomShapesInTheEditor scene.)

CustomShapesInTheEditor.unitypackage

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top