Перечислить более Tilemap.cellBounds.allPositionsWithin
, которое должно возвращать BoundsInt
в каждом цикле.Используйте HasTile
, чтобы проверить, есть ли тайл в этой позиции в Tilemap
.Если в этой позиции есть тайл, используйте Tilemap.CellToWorld
, чтобы преобразовать позицию в мир, затем добавьте его к List
.
List<Vector3> GetCellsFromTilemap(Tilemap tilemap)
{
List<Vector3> worldPosCells = new List<Vector3>();
foreach (var boundInt in tilemap.cellBounds.allPositionsWithin)
{
//Get the local position of the cell
Vector3Int relativePos = new Vector3Int(boundInt.x, boundInt.y, boundInt.z);
//Add it to the List if the local pos exist in the Tile map
if (tilemap.HasTile(relativePos))
{
//Convert to world space
Vector3 worldPos = tilemap.CellToWorld(relativePos);
worldPosCells.Add(worldPos);
}
}
return worldPosCells;
}
Для использования:
Tilemap Examplemap = ...;
List<Vector3> cells = GetCellsFromTilemap(Examplemap);
Если вы предпочитаете, чтобы позиции в ячейках возвращались в локальном пространстве, замените tilemap.CellToWorld(relativePos)
на tilemap.CellToLocal(relativePos)
.
List<Vector3> GetCellsFromTilemap(Tilemap tilemap)
{
List<Vector3> worldPosCells = new List<Vector3>();
foreach (var boundInt in tilemap.cellBounds.allPositionsWithin)
{
//Get the local position of the cell
Vector3Int relativePos = new Vector3Int(boundInt.x, boundInt.y, boundInt.z);
//Add it to the List if the local pos exist in the Tile map
if (tilemap.HasTile(relativePos))
{
//Convert to world space
Vector3 localPos = tilemap.CellToLocal(relativePos);
worldPosCells.Add(localPos);
}
}
return worldPosCells;
}
Наконец, если вы просто хотите Vector2Ints
без преобразования, топросто добавьте данные из цикла непосредственно в List
:
List<Vector3Int> GetCellsFromTilemap(Tilemap tilemap)
{
List<Vector3Int> cells = new List<Vector3Int>();
foreach (var boundInt in tilemap.cellBounds.allPositionsWithin)
{
//Get the local position of the cell
Vector3Int relativePos = new Vector3Int(boundInt.x, boundInt.y, boundInt.z);
//Add it to the List if the local pos exist in the Tile map
if (tilemap.HasTile(relativePos))
cells.Add(relativePos);
}
return cells;
}