skip to Main Content

Scripting Support (C#)

You can instantiate the prefab “Grids2D“ or add it to the scene from the Editor. Once the prefab is in the scene, you can access the functionality from code through the static instance property:

Using Grids2D;


void Start () {
Grid2D grid = Grid2D.instance;
...
}

Complete list of public properties, methods and events

Cell related

grid.numCells: desired number of cells. The actual generated cells will vary depending on the topology.

grid.columnCount: number of columns for boxed and hexagonal topologies

grid.rowCount: number of rows for boxed and hexagonal topologies

grid.cells: return a List<Cell> of all cells/states records.

grid.showCells: whether to show or not the cells borders.

grid.cellHighlighted: returns the cell/state object in the cells list under the mouse cursor (or null if none).

grid.cellHighlightedIndex: returns the cell/state index in the cells list under the mouse cursor (or -1 if none).

grid.cellLastClicked: returns the last clicked Cell object.

grid.cellLastClickedIndex: returns the index of last cell clicked.

grid.cellHighlightColor: color for the highlight of cells.

grid.cellBorderColor color for all cells borders.

grid.cellBorderAlpha: helper method to change only the alpha of cell borders.

grid.CellSetTag: assigns a cell a user-defined integer tag. Cell can be later retrieved very quickly with CellGetWithTag.

grid.CellGetWithTag: retrieves a cell previously tagged with an integer using the method CellSetTag.

grid.CellSetGroup: assigns a cell to a group.

grid.CellGetGroup: get the current group of a cell.

tgs.CellGetFromGroup: gets all cell indices from a group.

grid.CellGetIndex(Cell): returns the index of the provided Cell object in the Cells collection.

grid.CellGetIndex(row, column, clampToBorders): returns the index of the provided Cell by row and column. Use clampToBorders = true to limit row/column to edges of the grid or to allow wrap around edges otherwise.

grid.CellGetPosition(cellIndex): returns the center of a cell in the world space.

Grid.CellGetRectWorldSpace(int cellIndex): returns the world space bounds of a cell.

grid.CellGetVertexCount(cellIndex): returns the number of vértices of any cell.

grid.CellGetVertexPosition(cellIndex, vertexIndex): returns the world space position of any vertex of a cell.

grid.CellGetAtPosition(position): returns the cell that contains position (in local space coordinates).

grid.CellGetAtPosition(column, row): returns the cell located at given row and col (only boxed and hexagonal topologies).

grid.CellGetNeighbours(cellIndex): returns a list of neighbour cells to a specific cell.

grid.CellGetNeighbours(cellIndex, maxDistance, maxCost, cellGroupMask, canCrossCheck, sortByDistance): returns a list of neighbour cells which are nearer than certain distance to a specific cell, optionally with max crossing cost, cell group mask, canCrossCheck flag and sorting results by distance.

grid.CellMerge(Cell 1, Cell 2): merges two cells into the first one.

grid.CellToggle (cellIndex, visible, color, refreshGeometry): colorize one cell’s surface with specified color. Use refreshGeometry to ignore cached surfaces (usually you won’t do this).

tgs.CellToggle(cellIndex, visible, color, refreshGeometry, texture, useCanvasRect): color and texture one cell’s surface with specified color. Use refreshGeometry to ignore cached surfaces (usually you won’t do this). Texture can be scaled, panned and/or rotated adding optional parameters to this function call. Finally, “useCanvasRect” can be used to draw a portion of the texture inside the cell.

grid.CellHideRegionSurface(cellIndex): uncolorize/clears one cell’s surface.

grid.CellFadeOut(cellIndex, color, duration): colorizes a cell and fades it out for duration in seconds.

grid.CellFlash(cellIndex, color, duration): colorizes a cell and flashes it out for duration in seconds.

grid.CellBlink(cellIndex, color, duration): colorizes a cell and blinks it out for duration in seconds.

grid.CellSetTerritory(cellIndex, territoryIndex): changes the territory of a cell and updates boundaries.

grid.CellGetColor(cellIndex): returns current cell’s fill color.

grid.CellGetTexture(cellIndex): returns current cell’s fill texture.

grid.CellSetTexture(cellIndex, texture): assigns a new texture to the cell.

grid.CellIsVisible: returns true if the cell is visible.

grid.CellSetVisible: makes the cell visible or invisible. Call Redraw() to update the grid afterwards.

grid.CellSetBorderVisible: makes the cell’s border visible or invisible. Call Redraw() to update the grid afterwards.

Territory related

grid.numTerritoriess: desired number of territories (1-number of cells).

grid.territories: return a List<Territory> of all territories.

grid.showTerritories: whether to show or not the territories borders.

grid.territoryHighlighted: returns the Territory object for the territory under the mouse cursor (or null).

grid.territoryHighlightedIndex: returns the index of territory under the mouse cursor (or -1 if none).

grid.territoryLastClickedIndex: returns the index of last territory clicked.

grid.territoryHighlightColor: color for the highlight of territories.

grid.territoryFrontiersColor: color for all territory frontiers.

grid.territoryFrontiersAlpha: helper method to change only the alpha of territory frontiers.

grid.colorizeTerritories: whether to fill or not the territories.

grid.colorizedTerritoriesAlpha: transparency level for colorized territories.

grid.allowTerritoriesInsideTerritories: this option will allow that a territory surrounds completely another territory. When enabled, the internal territory still can be selected, colorized, textured, …

grid.TerritoryGetIndex(territory): returns the index of the territory object in the Territories list.

grid.TerritoryToggle (territoryIndex, visible, color, refreshGeometry): colorize one cell’s surface with specified color. Use refreshGeometry to ignore cached surfaces (usually you won’t do this).

grid.TerritoryToggle(cellIndex, visible, color, refreshGeometry, texture, useCanvasRect): color and texture one territory’s surface with specified color. Use refreshGeometry to ignore cached surfaces (usually you won’t do this). Texture can be scaled, panned and/or rotated adding optional parameters to this function call. Finally, “useCanvasRect” can be used to draw a portion of the texture inside the cell.

grid.TerritoryHide (territoryIndex): uncolorize/clears one territory’s surface.

grid.TerritoryGetNeighbours(territoryIndex): returns a list of neighbour territories to a specific territory.

grid.TerritoryGetAtPosition(position): returns the territory that contains position (in local space coordinates).

grid.TerritoryGetPosition(position): returns the territory center in world space coordinates.

grid.TerritoryGetRectWorldSpace(int territoryIndex): returns the world space bounds of a territory.

grid.TerritoryFadeOut(cellIndex, color, duration): colorizes a territory and fades it out for duration in seconds.

grid.TerritoryFlash(cellIndex, color, duration): colorizes a territory and flashes it out for duration in seconds.

grid.TerritoryBlink(cellIndex, color, duration): colorizes a territory and blinks it out for duration in seconds.

grid.CreateTerritories(texture, neutralColor): automatically generates territories based on a color texture. Use neutral color to ignore areas.

grid.TerritoryIsVisible(territoryIndex): returns true if territory is visible.

grid.TerritorySetVisible(territoryIndex): makes a territory visible or invisible.

grid.TerritorySetBorderVisible(territoryIndex): makes a territory’s border visible or invisible.

User interaction

grid.highlightMode: choose between None, Territories or Cells. Defaults to Cells.

grid.highlightFadeAmount: alpha range for the fading effect of highlighted cells/territories (0-1).

grid.overlayMode: decide whether the highlight is shown always on top or takes into account z-buffer which will make it more fancy and part of the scene.

Grid

grid.seed: seed constant for the random generator.

grid.gridTopology: type of cells (Irregular, Box, Hexagonal, …)

grid.gridRelaxation: relaxation factor for the irregular topology (defaults 1). It creates more homogeneous irregular grids but has a performance hit during the creation of the grid (not afterwards).

grid.gridCurvature: will bend cell edges for all topologies. It will add more vertex count to the mesh so it’s only available on grids with less than 100 cells.

grid.gridDepthOffset: Z-Buffer offset for the grid system shaders. Very cheap method to prevent z-fighting which can be combined with above methods. You should use this property first to adjust your grid.

grid.voronoiSites: allows you to set the centers of the Voronoi cells.

Path Finding / LoS related

grid.CellSetCanCross: specifies if this cell blocks any path.

grid.CellSetCrossCost: specifies the crossing cost for entering a cell.

grid.CellSetSideCrossCost: specifies the crossing cost for a given side of the cell.

grid.FindPath(cellStartIndex, cellEndIndex, maxCost): returns a list of cell indices that form the path with an optional maximum search cost.

grid.CellGetNeighbours(cellIndex, distance): returns a list of neighbour cells which are nearer than certain distance to a specific cell.

grid.CellGetLineOfSight(startIndex, endIndex, ref cellindices, ref worldPosition, groupMask, lineResolution, exhaustiveCheck): returns true if LoS is available between cell at startIndex and cell at endIndex (also returns additional useful info).

grid.CellSetSideBlocksLOS(cellIndex, side, state): set a cell side to block Line of Sight.

grid.CellGetSideBlocksLOS(cellIndex, side): returns if a cell side is blocking Line of Sight.

Other useful methods

grid.instance: reference to the actual Grids 2D System component on the scene.

grid.Redraw: forces a redraw of the grid mesh.

grid.DrawLine: draws a line that connect two vertices of a cell or two cells by their centers.

grid.MoveTo: moves a given game object to a destination cell with a custom velocity and optional separation from grid. If velocity is 0, displacement is immediate. Use the OnMoveEnd event to get notified when the gameobject reaches destination, for example:

grid.MoveTo (piece, row, column, 0.5f).OnMoveEnd += (gameObject) => { Debug.Log(“Piece moved to ” + row + “, “ + column); };

grid.MovePause: pauses a moving gameobject.

grid.MoveResume: pauses a moving gameobject.

grid.MovePauseToggle: pauses or resumes a moving/paused gameobject.

Events

grid.OnCellEnter: triggered when pointer enters a cell.

grid.OnCellExit: triggered when pointer exits a cell.

grid.OnCellClick: triggered when user clicks or taps a cell (visible or not).

grid.OnCellMouseUp: triggered when releasing the mouse button on a cell (visible or not).

grid.OnCellHighlight: triggered before highlighting cell occurs, allows cancel highlighting.

grid.OnTerritoryEnter: triggered when pointer enters a territory.

grid.OnTerritoryExit: triggered when pointer exits a territory.

grid.OnTerritoryClick: triggered when user clicks or taps a territory.

grid.OnTerritoryHighlight: triggered before highlighting territory occurs, allows cancel highlighting.

grid.OnPathFindingCrossCell: triggered when path finding algorithm estimates cost to this cell. You can use this event to return extra cost and customize path results.

Handling cells and territories

Please, refer to demo scripts included in demo scenes for example of API usage, like merging and toggling cells visibility, …

Back To Top