0

I want to have all of the cells in a Tilemap into an array, I've tried:

Vector3Int[] Example = Examplemap.cellBounds

but that didn't work. Please help me

3
  • well don't you already have them all? if cellBounds is the boundry of the tile map then your Vector2Int are literally every value between cellBounds.xMin - > cellBounds.xMax and cellBounds.yMin -> cellBounds.yMax...
    – AresCaelum
    Oct 2, 2018 at 19:23
  • Thanks, I'll see if i can use that Oct 2, 2018 at 19:28
  • Ok sorry I don't understand how to use that, can you provide an example of how I can get all the Vector2ints from the cell bounds Oct 2, 2018 at 19:40

1 Answer 1

1

Enumerate over Tilemap.cellBounds.allPositionsWithin which should return BoundsInt in each loop. Use the HasTile to check if there is a tile that position in the Tilemap. If there is a tile in that position, use Tilemap.CellToWorld to covert the position to world then add it to a List.

List<Vector3> GetCellsFromTilemap(Tilemap tilemap)
{
    List<Vector3> worldPosCells = new List<Vector3>();
    foreach (var boundInt in tilemap.cellBounds.allPositionsWithin)
    {
        //Get the local position of the cell
        Vector3Int relativePos = new Vector3Int(boundInt.x, boundInt.y, boundInt.z);
        //Add it to the List if the local pos exist in the Tile map
        if (tilemap.HasTile(relativePos))
        {
            //Convert to world space
            Vector3 worldPos = tilemap.CellToWorld(relativePos);
            worldPosCells.Add(worldPos);
        }
    }
    return worldPosCells;
}

To use:

Tilemap Examplemap = ...;
List<Vector3> cells = GetCellsFromTilemap(Examplemap);

If you prefer the cell positions to be returned in local space, replace tilemap.CellToWorld(relativePos) with tilemap.CellToLocal(relativePos).

List<Vector3> GetCellsFromTilemap(Tilemap tilemap)
{
    List<Vector3> worldPosCells = new List<Vector3>();
    foreach (var boundInt in tilemap.cellBounds.allPositionsWithin)
    {
        //Get the local position of the cell
        Vector3Int relativePos = new Vector3Int(boundInt.x, boundInt.y, boundInt.z);
        //Add it to the List if the local pos exist in the Tile map
        if (tilemap.HasTile(relativePos))
        {
            //Convert to world space
            Vector3 localPos = tilemap.CellToLocal(relativePos);
            worldPosCells.Add(localPos);
        }
    }
    return worldPosCells;
}

Finally, if you just want the Vector2Ints with no conversion then just add the data from the loop directly to the List:

List<Vector3Int> GetCellsFromTilemap(Tilemap tilemap)
{
    List<Vector3Int> cells = new List<Vector3Int>();
    foreach (var boundInt in tilemap.cellBounds.allPositionsWithin)
    {
        //Get the local position of the cell
        Vector3Int relativePos = new Vector3Int(boundInt.x, boundInt.y, boundInt.z);
        //Add it to the List if the local pos exist in the Tile map
        if (tilemap.HasTile(relativePos))
            cells.Add(relativePos);
    }
    return cells;
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.