0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Unity 6.4で追加予定のTilemap.GetTilesのwithinBounds引数の説明と挙動を調べる

0
Posted at

Unity 6.4で追加予定のTilemap.GetTilesの引数withinBoundsの説明と挙動に気になる点があります。

※ Unity 6.4は本稿執筆時点の2026/02/27(金)時点の情報です。実際の挙動は Unity 6.4 beta 9のものです。

本稿執筆時点において、Tilemap.GetTilesメソッドの公式リファレンスには、次のようなオーバーロードのメソッドシグネチャと、

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

次のようなサンプルコードと、

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

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.

メソッドの補足説明が掲載されています。

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.


ところで、メソッドの補足説明における「withinBoundsがtrue」の場合の部分は、説明が間違っていそうです。

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.

まず、サンプルコード中のnew BoundsInt(0, 0, 0, 5, 5, 1)は、minが(0, 0, 0)で、sizeが(5, 5, 1)です。new BoundsInt(0, 0, 0, 5, 5, 1).allPositionsWithinを列挙すると、(0, 0, 0)、(1, 0, 0)・・・(3, 4, 0)、(4, 4, 0)となります。(5, 5, 1)や(5, 5, 0)は列挙されません。

だから、この文は、

If withinBounds is set to True, this returns all tiles within (0, 0, 0) to (4, 4, 0), where x and y are between 0 and 4.

が正しいと思います。

実際にUnity 6.4 Beta 9において、次のように(0, 0, 0)から(6, 6, 0)までタイルを敷き詰めたTilemapに対して、引数boundsとしてnew BoundsInt(0, 0, 0, 5, 5, 1)を、引数withinBoundsとしてtrueを渡したところ、Tilemap.GetTileは(0, 0, 0)から(4, 4, 0)までの25個のタイルを取得していました。

スクリーンショット 2026-02-28 004033.png

おそらく、説明文が適切ではないと思います。


次に、withinBoundsがfalseの場合の説明です。

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.

new BoundsInt(0, 0, 0, 5, 5, 1)なBoundsIntと、(6, 0, 0)と(-4, 5, 0)は、before the start of the given bounds or after the end of the given bounds.ではないと思います。

また、実際にwithinBoundsがfalseの時の挙動は私が期待・予測したものではありませんでした。

スクリーンショット 2026-02-28 002546.png

次のような(0, 0, 0)から(2, 2, 0)までタイルを配置したTilemapに対して、引数boundsとしてnew BoundsInt(1, 1, 0, 1, 1, 1)を、引数withinBoundsとしてfalseを渡したところ、Tilemap.GetTileは次の位置にあるタイルを取得しました。

  • (1, 1, 0)
  • (2, 1, 0)
  • (0, 2, 0)
  • (1, 2, 0)
  • (2, 2, 0)

また同じTilemapに対して、引数boundsとしてnew BoundsInt(0, 0, 0, 1, 1, 1)を、引数withinBoundsとしてfalseを渡したところ、Tilemap.GetTileは次の位置にあるタイルを取得しました。

  • (0, 0, 0)
  • (1, 0, 0)
  • (2, 0, 0)
  • (0, 1, 0)
  • (1, 1, 0)

they either come before the start of the given bounds or after the end of the given bounds.

あたりの説明から、おそらく引数に渡したbounds上のタイル、それに加えてboundsの隣接セル上のタイルを追加で取得するメソッドだと予想したのですが、どうやら違いそうです。


また個人的には、Tilemap.GetTilesメソッドのwithinBoudns引数は必要ないと考えています。withinBoundsがfalseの時の挙動が、引数名から予測しにくいからです。boundsよりも広い範囲のタイルを取得したいなら、素直に引数に渡すboundsを変えればよいと考えています。


公式ディスカッションにもフィードバックを送りました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?