Working with GridBehaviours

GridBehaviours* are what you use to add more behavior to your grid. With GridBehaviours, you can

  • get access the grid (as a data structure) and the associated map
  • hook in your own grid initialization code that also runs in the editor
  • get notified of mouse events

(* We follow Unity in using the British spelling despite American spellings for everything else.)

To use a GridBehaviour, you must extend from this class. GridBehaviour takes a type parameter denoting the point type of the grid. (Use the Grid Index if you are unsure which points to use).

For example, here is an empty implementation of a GridBehaviour for a PointyHexGrid:

public class MyGrid: GridBehaviour
{
}

Accessing the Grid and Map

GridBehaviour provides properties with which you can access the grid and map.

  • Grid of type IGrid
  • Map of type IMap3D

where TPoint is whatever the type parameter is of the GridBehaviour you extend from

These are the generic interfaces. They support all the common functionality, but at times, you may need to cast them to a specific grid or map. You may also need to cast them in the cell type to get more specific cell operations.

For example, if you implement a rectangular grid, your GridBehaviour will extend from GridBehaviour. You can then cast the grid and maps as follows:

RectGrid rectGrid = (RectGrid) Grid;
IGrid gridWithSpriteCells = rectGrid.CastValues();
RectGrid rectGridWithSpriteCells = (RectGrid) gridWithSpriteCells;

You can also get hold of the grid builder through the GridBuilder property, although you are unlikely to need this.

Hooking up your own initialization code

Once you have implemented your own GridBehaviour, you can override the InitGrid method, and put your initialization code there. The following example will make the cell at (0, 0) black.

override public void InitGrid()
{
   Grid[PointyHexPoint.Zero].Color = Color.black;
}
  • Remember to add the override modifier; otherwise, this function will not be called.
  • If you want your initialization code to run without errors in the editor, mark your script as ExecuteInEdit mode.

Mouse Events

If you marked your grid as Interactive in the grid builder, you will get notified when the user clicks on your grid.

(This is only useful for 2D games where the grid is parallel to the camera. For other cases, you need to implement a raycasting method yourself).

The following mouse events are supported:

  • OnClick Called when any mouse button has been clicked
  • OnLeftClick Called when the left mouse button has been clicked
  • OnRightClick Called when the right mouse button has been clicked
  • OnMiddleClick Called when the middle mouse button has been clicked

The property MousePosition gives the position of the mouse in grid coordinates. It can be used even when the grid is not marked as interactive.

The following example shows how to make a clicked cell red:

public void OnClick()
{
   Grid[MousePosition].Color = Color.red;
}

Leave a Reply

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

Scroll to Top