Version: Unity 6.4 Alpha (6000.4)
LanguageEnglish
  • C#

Tilemap.GetTiles

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public Tilemaps.Tilemap.TileArray GetTiles(BoundsInt bounds, Unity.Collections.Allocator allocator);

Parameters

Parameter Description
bounds The bounds to retrieve from.
allocator The Allocator type used to allocate the memory for the TileArray. You must use Allocator.Temp, Allocator.Domain, or Allocator.Persistent. The default value is Allocator.Temp.

Returns

TileArray A TileArray containing all the TileBase instances in the bounds.

Description

Retrieves all tiles within the given bounds as a TileArray .

Use this method to efficiently retrieve tiles as a batch, rather than calling 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 console
using 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"); } } }

Declaration

public Tilemaps.Tilemap.TileArray GetTiles(BoundsInt bounds, Unity.Collections.MemoryLabel memoryLabel);

Parameters

Parameter Description
bounds The bounds to retrieve from.
memoryLabel Memory label used for profiling and tracking this memory allocation in Unity.

Returns

TileArray TileArray containing the TileBases in the bounds.

Description

Retrieves a TileArray within the given bounds.

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

// Retrieves all Tiles from an area on the Tilemap and prints out the Tiles to console
using 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 var tiles = tilemap.GetTiles(area, kMemoryLabel); // Will call TileArray.Dispose() once it is out of scope foreach (var tile in tiles) { print(tile != null ? tile.name : "Empty"); } } }

Declaration

public int GetTiles(BoundsInt bounds, out Tilemaps.Tilemap.PositionArray positions, out Tilemaps.Tilemap.TileArray tiles, Unity.Collections.Allocator allocator, bool withinBounds);

Parameters

Parameter Description
bounds The bounds to retrieve from.
positions A PositionArray containing the position of each TileBase in the bounds.
tiles A TileArray containing all the TileBase instances in the bounds.
allocator The Allocator type used to allocate the memory for the 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. The default value is True. If True, only tiles located within the tilemap’s defined bounds are returned. If False, tiles outside the tilemap’s bounds may also be returned if they fall within the given bounds.

Returns

int Returns the number of positions and Tiles retrieved.

Description

Retrieves all tiles within the given bounds as a TileArray, with their corresponding positions in a PositionArray. Only positions containing a Tile are included.

Use this method to efficiently retrieve tiles as a batch, rather than calling 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 console
using 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>(); var count = tilemap.GetTiles(area, out var positions, out var tiles); for (var 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 within (0, 0, 0) to (5, 5, 1), where x and y are between 0 and 5. If withinBounds is set to False, tiles outside the Tilemap's bounds might be included if they fall within 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.


Declaration

public int GetTiles(BoundsInt bounds, out Tilemaps.Tilemap.PositionArray positions, out Tilemaps.Tilemap.TileArray tiles, Unity.Collections.MemoryLabel memoryLabel, bool withinBounds);

Parameters

Parameter Description
bounds The bounds to retrieve from.
positions A PositionArray containing the position of each TileBase in the bounds.
tiles A 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

int Returns the number of positions and Tiles retrieved.

Description

Retrieves all tiles within the given bounds as a TileArray, with their corresponding positions in a PositionArray. Only positions containing a Tile are included.

Use this method to efficiently retrieve tiles as a batch, rather than calling 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 console
using 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(); } }