Skip to main content

CellDataStore

This class acts as the storage container for all cell data.

See a note on performance as to why the dictionary storage instead of an array implementation.

Public methods

GetDict

public Dictionary<Vector3Int, CellData> GetDict()

Returns the internal dictionary. Could be used for saving data.


SetDict

public void SetDict(Dictionary<Vector3Int, CellData> gridData)

Sets the internal dictionary. Could be used for loading data.


AddCell

public bool AddCell(Vector3Int position)
public bool AddCell(Vector3Int position, CellData cellData)

Adds cell data at the given position. Returns true if position was not previously in use and cell data was added.

If no CellData is provided, the tilemaps are scanned until the correct CellData is found based on TilemapRules. Returns false if no tile is present at the position, no rule is found based on the tile and if the position is already in use by a different cell.

caution

There can only be one cell data object at any given grid position.

Parameters

  • position: The grid position to add cell data to.
  • cellData: The cell data object you wish to add.

Example

var gridPosition = new Vector3Int(0, 0, 0);
//Loads the cell data object
var cellData = Resources.Load<CellData>("/Cells/MyCellData");
//Adds the cell data and returns true if successful
var wasAdded = tilemapExtended.DataCells.AddCell(gridPosition, cellData);

if (wasAdded) { /*...*/ }

...

//Tries to automatically add CellData based on the provided TilemapRules
var wasAdded = tilemapExtended.DataCells.AddCell(gridPosition);

if (wasAdded) { /*...*/ }

ContainsCell

public bool ContainsCell(Vector3Int position)

Returns true if the store contains a cell at the given position.

Parameters

  • position: The grid position to check if a cell exists.

Example

var gridPosition = new Vector3Int(0, 0, 0);
//Check if a cell exists at the given position
var exists = tilemapExtended.DataCells.ContainsCell(gridPosition);

if (exists) { /*...*/ }

GetAllCells

public CellData[] GetAllCells()

Returns an array containing all cells from the store.

Example

//Returns all cells from the store
var cells = tilemapExtended.DataCells.GetAllCells();

foreach (var cell in cells) {
//Do something with cells
}

GetCell

public CellData GetCell(Vector3Int position)

Returns the cell at the specified grid position. Returns null if no cell is found.

Parameters

  • position: The grid position of the cell.

Example

var gridPosition = new Vector3Int(0, 0, 0);
//Returns the cell data at grid position x: 0, y: 0, z: 0
var cell = tilemapExtended.DataCells.GetCell(gridPosition);

//Or when extending CellData with a custom data class
var cellData = tilemapExtended.DataCells.GetCell(gridPosition) as MyCustomClass;

GetExistingCells

public List<CellData> GetExistingCells(IEnumerable<Vector3Int> positions)

Returns valid, stored tiles from an enumeration of positions. This method is useful if you sweep a larger area and want to make sure you only get valid cells back.

Parameters

  • positions: The positions of the cells to check and retrieve.

Example

//Define area from (-2, -2) to (2, 2)
var bounds = new BoundsInt(new Vector3Int(-2, -2, 0), new Vector3Int(2, 2, 0));
//Get only valid cells within those bounds
var cells = tilemapExtended.DataCells.GetExistingCells(bounds.allPositionsWithin);

GetNumCells

public int GetNumCells()

Returns the number of cells currently held in the store.


RemoveCell

public bool RemoveCell(Vector3Int position)

Removes cell data at the given position. Returns true if cell data was present and removed successfully.

Parameters

  • position: The grid position to remove cell data from.

Example

var gridPosition = new Vector3Int(0, 0, 0);
var wasRemoved = tilemapExtended.DataCells.RemoveCell(gridPosition);

if (wasRemoved) { /*...*/ }

ReplaceCell

public void ReplaceCell(Vector3Int position, CellData cellData)

Replaces existing cell data at the given position or adds it if the positions did not contain data previously.

Parameters

  • position: The grid position to replace cell data at.
  • cellData: The new cellData to be placed at the given position.

Example

var gridPosition = new Vector3Int(0, 0, 0);
//Load some cell data
var cellData = Resources.Load<CellData>("/Cells/MyCellData");
//Replace the existing data at position (0, 0, 0) with the newly loaded data
tilemapExtended.DataCells.ReplaceCell(gridPosition, cellData);