To make your own grid shape:
- Extend a new class from
CustomGridBuilder
. - Override the
MakeGrid
method, and return the grid of the required shape from this method. - 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 IGridMakeGrid () { 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.)