Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


GetTiles

Retrieves all tiles within the given bounds as a Tilemap.TileArray.
Read time 6 minutesLast updated 6 days ago

Definition

GetTiles(BoundsInt, Allocator)

Retrieves all tiles within the given bounds as a Tilemap.TileArray.
public Tilemap.TileArray GetTiles(BoundsInt bounds, Allocator allocator = Allocator.Temp)

Parameters

bounds

The bounds from which to retrieve the tiles.

allocator

The Allocator type used to allocate the memory for the Tilemap.TileArray. You must use Allocator.Temp, Allocator.Domain, or Allocator.Persistent. The default value is Allocator.Temp.

Returns

Type

Description

TileArrayA Tilemap.TileArray containing all the TileBase instances in the bounds.

Remarks

Use this method to efficiently retrieve tiles as a batch, rather than calling Tilemap.GetTile for each position. This can significantly reduce overhead when processing large areas.

Examples

// Retrieves all Tiles from an area on the Tilemap and prints out the Tiles to consoleusing UnityEngine;using UnityEngine.Tilemaps;public class TilemapExample1 : MonoBehaviour{ public BoundsInt area; void GetTilesExample1() { Tilemap tilemap = GetComponent<Tilemap>(); using var tiles = tilemap.GetTiles(area); // Will call TileArray.Dispose() once it is out of scope foreach (var tile in tiles) { print(tile != null ? tile.name : "Empty"); } }}

GetTiles(BoundsInt, MemoryLabel)

Retrieves a Tilemap.TileArray within the given bounds.
public Tilemap.TileArray GetTiles(BoundsInt bounds, MemoryLabel memoryLabel)

Parameters

bounds

The bounds to retrieve from.

memoryLabel

Memory label used for profiling and tracking this memory allocation in Unity.

Returns

Type

Description

TileArrayTilemap.TileArray containing the TileBases in the bounds.

Remarks

This is meant for more a performant way to get Tiles as a batch, when compared to calling Tilemap.GetTile for every single position.

Examples

// Retrieves all Tiles from an area on the Tilemap and prints out the Tiles to consoleusing Unity.Collections;using UnityEngine;using UnityEngine.Tilemaps;public class TilemapExample2 : MonoBehaviour{ static readonly MemoryLabel kMemoryLabel = new MemoryLabel("TilemapExample", "Get", Allocator.Domain); public BoundsInt area; void GetTilesExample2() { Tilemap tilemap = GetComponent<Tilemap>(); using Tilemap.TileArray tiles = tilemap.GetTiles(area, kMemoryLabel); // Will call TileArray.Dispose() once it is out of scope foreach (TileBase tile in tiles) { print(tile != null ? tile.name : "Empty"); } }}

GetTiles(BoundsInt, PositionArray, TileArray, Allocator, bool)

Retrieves all tiles within the given bounds as a Tilemap.TileArray, with their corresponding positions in a Tilemap.PositionArray. Only positions containing a Tile are included.
public int GetTiles(BoundsInt bounds, out Tilemap.PositionArray positions, out Tilemap.TileArray tiles, Allocator allocator = Allocator.Temp, bool withinBounds = true)

Parameters

bounds

The bounds from which to retrieve the tiles.

positions

A Tilemap.PositionArray containing the position of each TileBase in the bounds.

The Tilemap.TileArray containing all the TileBase instances in the bounds.

allocator

The Allocator type used to allocate the memory for the Tilemap.TileArray. You must use Allocator.Temp, Allocator.Domain, or Allocator.Persistent. The default value is Allocator.Temp.

withinBounds

Whether to retrieve the tiles within the given bounds.

Returns

Type

Description

intThe number of positions and tiles retrieved.

Remarks

Use this method to efficiently retrieve tiles as a batch, rather than calling Tilemap.GetTile for each position. This can significantly reduce overhead when processing large areas.
// Retrieves all Tiles from an area on the Tilemap and prints out the Tiles to consoleusing UnityEngine;using UnityEngine.Tilemaps;public class TilemapExample3 : MonoBehaviour{ public BoundsInt area = new BoundsInt(0, 0, 0, 5, 5, 1); void GetTilesExample3() { Tilemap tilemap = GetComponent<Tilemap>(); int count = tilemap.GetTiles(area, out Tilemap.PositionArray positions, out Tilemap.TileArray tiles); for (int i = 0; i < count; i++) { print($"Position: {positions[i]}, Tile: {tiles[i].name}"); } // Manually dispose allocated arrays positions.Dispose(); tiles.Dispose(); }}
If
withinBounds
is set to
true
, this returns all tiles with the positions from
bounds.allPositionsWithin
. These positions are within the bounds of (0, 0, 0) to (5, 5, 1), where
x
and
y
are between 0 and 4, inclusive. If
withinBounds
is set to
false
, this returns all tiles from the start of the bounds (0, 0, 0) to the end of the bounds (5, 5, 1) inclusive. Tiles outside the Tilemap's bounds might be included if they fall within the start and end of the given bounds. Positions such as (6, 0, 0), (-1, 1, 0), or (-4, 5, 0) are included, but positions such as (-2, 0, 0) or (6, 6, 0) are excluded because they either come before the start of the given bounds or after the end of the given bounds.

GetTiles(BoundsInt, PositionArray, TileArray, MemoryLabel, bool)

Retrieves all tiles within the given bounds as a Tilemap.TileArray, with their corresponding positions in a Tilemap.PositionArray. Only positions containing a Tile are included.
public int GetTiles(BoundsInt bounds, out Tilemap.PositionArray positions, out Tilemap.TileArray tiles, MemoryLabel memoryLabel, bool withinBounds = true)

Parameters

bounds

The bounds from which to retrieve the tiles.

positions

A Tilemap.PositionArray containing the position of each TileBase in the bounds.

The Tilemap.TileArray containing all the TileBase instances in the bounds.

memoryLabel

Memory label used for profiling and tracking this memory allocation in Unity.

withinBounds

Whether to retrieve the tiles within the given bounds. The default value is True.

Returns

Type

Description

intThe number of positions and tiles retrieved.

Remarks

Use this method to efficiently retrieve tiles as a batch, rather than calling Tilemap.GetTile for each position. This can significantly reduce overhead when processing large areas.

Examples

// Retrieves all Tiles from an area on the Tilemap and prints out the Tiles to consoleusing Unity.Collections;using UnityEngine;using UnityEngine.Tilemaps;public class TilemapExample4 : MonoBehaviour{ static readonly MemoryLabel kMemoryLabel = new MemoryLabel("TilemapExample", "Get", Allocator.Domain); public BoundsInt area = new BoundsInt(0, 0, 0, 5, 5, 1); void GetTilesExample4() { Tilemap tilemap = GetComponent<Tilemap>(); var count = tilemap.GetTiles(area, out var positions, out var tiles, kMemoryLabel); for (var i = 0; i < count; i++) { print($"Position: {positions[i]}, Tile: {tiles[i].name}"); } // Manually dispose allocated arrays positions.Dispose(); tiles.Dispose(); }}