# GetTiles

> Retrieves all tiles within the given bounds as a Tilemap.TileArray.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine.Tilemaps](/engine/6000.7/script-reference/unityengine/tilemaps.md)
* **Assembly:** UnityEngine.TilemapModule

## GetTiles(BoundsInt, Allocator)

Retrieves all tiles within the given bounds as a [Tilemap.TileArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/tilearray.md).

```csharp
public Tilemap.TileArray GetTiles(BoundsInt bounds, Allocator allocator = Allocator.Temp)
```

### Parameters

**** (\[BoundsInt]\(/engine/6000.7/script-reference/unityengine/boundsint)): The bounds from which to retrieve the tiles.**** (\[Allocator]\(/engine/6000.7/script-reference/unity/collections/allocator)): The [Allocator](/engine/6000.7/script-reference/unity/collections/allocator.md) type used to allocate the memory for the [Tilemap.TileArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/tilearray.md).  You must use [Allocator.Temp](/engine/6000.7/script-reference/unity/collections/allocator/temp.md), [Allocator.Domain](/engine/6000.7/script-reference/unity/collections/allocator/domain.md), or [Allocator.Persistent](/engine/6000.7/script-reference/unity/collections/allocator/persistent.md). The default value is [Allocator.Temp](/engine/6000.7/script-reference/unity/collections/allocator/temp.md).

### Returns

| Type                                                                                   | Description                                                                                                                                                                                                               |
| -------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [TileArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/tilearray.md) | A [Tilemap.TileArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/tilearray.md) containing all the [TileBase](/engine/6000.7/script-reference/unityengine/tilemaps/tilebase.md) instances in the bounds. |

### Remarks

Use this method to efficiently retrieve tiles as a batch, rather than calling [Tilemap.GetTile](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/gettile.md) for each position. This can significantly reduce overhead when processing large areas.

### Examples

```csharp
 // 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"); 
        }
    }
}
```

## GetTiles(BoundsInt, MemoryLabel)

Retrieves a [Tilemap.TileArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/tilearray.md) within the given bounds.

```csharp
public Tilemap.TileArray GetTiles(BoundsInt bounds, MemoryLabel memoryLabel)
```

### Parameters

**** (\[BoundsInt]\(/engine/6000.7/script-reference/unityengine/boundsint)): The bounds to retrieve from.**** (\[MemoryLabel]\(/engine/6000.7/script-reference/unity/collections/memorylabel)): Memory label used for profiling and tracking this memory allocation in Unity.

### Returns

| Type                                                                                   | Description                                                                                                                                                                                                |
| -------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [TileArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/tilearray.md) | [Tilemap.TileArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/tilearray.md) containing the [TileBase](/engine/6000.7/script-reference/unityengine/tilemaps/tilebase.md)s in the bounds. |

### Remarks

This is meant for more a performant way to get Tiles as a batch, when compared to calling [Tilemap.GetTile](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/gettile.md) for every single position.

### Examples

```csharp
 // 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 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](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/tilearray.md), with their corresponding positions in a [Tilemap.PositionArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/positionarray.md). Only positions containing a [Tile](/engine/6000.7/script-reference/unityengine/tilemaps/tile.md) are included.

```csharp
public int GetTiles(BoundsInt bounds, out Tilemap.PositionArray positions, out Tilemap.TileArray tiles, Allocator allocator = Allocator.Temp, bool withinBounds = true)
```

### Parameters

**** (\[BoundsInt]\(/engine/6000.7/script-reference/unityengine/boundsint)): The bounds from which to retrieve the tiles.**** (\[PositionArray]\(/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/positionarray)): A [Tilemap.PositionArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/positionarray.md) containing the position of each [TileBase](/engine/6000.7/script-reference/unityengine/tilemaps/tilebase.md) in the bounds.**** (\[TileArray]\(/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/tilearray)): The [Tilemap.TileArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/tilearray.md) containing all the [TileBase](/engine/6000.7/script-reference/unityengine/tilemaps/tilebase.md) instances in the bounds.**** (\[Allocator]\(/engine/6000.7/script-reference/unity/collections/allocator)): The [Allocator](/engine/6000.7/script-reference/unity/collections/allocator.md) type used to allocate the memory for the [Tilemap.TileArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/tilearray.md).  You must use [Allocator.Temp](/engine/6000.7/script-reference/unity/collections/allocator/temp.md), [Allocator.Domain](/engine/6000.7/script-reference/unity/collections/allocator/domain.md), or [Allocator.Persistent](/engine/6000.7/script-reference/unity/collections/allocator/persistent.md). The default value is [Allocator.Temp](/engine/6000.7/script-reference/unity/collections/allocator/temp.md).**** (\[bool]\(https\://learn.microsoft.com/dotnet/api/system.boolean)): Whether to retrieve the tiles within the given bounds.

### Returns

| Type                                                       | Description                                  |
| ---------------------------------------------------------- | -------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The number of positions and tiles retrieved. |

### Remarks

Use this method to efficiently retrieve tiles as a batch, rather than calling [Tilemap.GetTile](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/gettile.md) for each position. This can significantly reduce overhead when processing large areas.

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](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap.md)'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.

### Examples

```csharp
 // 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>();
        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();
    }
}
```

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

Retrieves all tiles within the given bounds as a [Tilemap.TileArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/tilearray.md), with their corresponding positions in a [Tilemap.PositionArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/positionarray.md). Only positions containing a [Tile](/engine/6000.7/script-reference/unityengine/tilemaps/tile.md) are included.

```csharp
public int GetTiles(BoundsInt bounds, out Tilemap.PositionArray positions, out Tilemap.TileArray tiles, MemoryLabel memoryLabel, bool withinBounds = true)
```

### Parameters

**** (\[BoundsInt]\(/engine/6000.7/script-reference/unityengine/boundsint)): The bounds from which to retrieve the tiles.**** (\[PositionArray]\(/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/positionarray)): A [Tilemap.PositionArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/positionarray.md) containing the position of each [TileBase](/engine/6000.7/script-reference/unityengine/tilemaps/tilebase.md) in the bounds.**** (\[TileArray]\(/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/tilearray)): The [Tilemap.TileArray](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/tilearray.md) containing all the [TileBase](/engine/6000.7/script-reference/unityengine/tilemaps/tilebase.md) instances in the bounds.**** (\[MemoryLabel]\(/engine/6000.7/script-reference/unity/collections/memorylabel)): Memory label used for profiling and tracking this memory allocation in Unity.**** (\[bool]\(https\://learn.microsoft.com/dotnet/api/system.boolean)): Whether to retrieve the tiles within the given bounds. The default value is True.

### Returns

| Type                                                       | Description                                  |
| ---------------------------------------------------------- | -------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The number of positions and tiles retrieved. |

### Remarks

Use this method to efficiently retrieve tiles as a batch, rather than calling [Tilemap.GetTile](/engine/6000.7/script-reference/unityengine/tilemaps/tilemap/gettile.md) for each position. This can significantly reduce overhead when processing large areas.

### Examples

```csharp
 // 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();
    }
}
```
