コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】Tilemap のすべての Position を取得する拡張メソッド

ソースコード

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public static class TilemapExtensions
{
    public static IEnumerable<Vector3Int> GetPositions( this Tilemap self )
    {
        var bound = self.cellBounds;

        for ( var x = bound.min.x; x < bound.max.x; ++x )
        {
            for ( var y = bound.min.y; y < bound.max.y; ++y )
            {
                yield return new Vector3Int( x, y, 0 );
            }
        }
    }
}

(Z 座標は未対応)

使用例

foreach ( var position in tilemap.GetPositions() )
{
    var tile = tilemap.GetTile<RuleTile>( position );

    Debug.Log( tile );
}