【问题标题】:Unity Tilemap get Vector3 world position from x and y indexesunity Tilemap 从 x 和 y 索引获取 Vector3 世界位置
【发布时间】:2018-07-20 03:42:38
【问题描述】:

我正在尝试了解 Tilemap,特别是我希望能够使用 Tilemap 的 x、y 索引来获得 Vector3 位置。我看到您可以从 Tilemap 中获取 TileBase 数组,但其中似乎没有任何实际数据可以提供此信息。

我还看到有一个 TileData 类,但我不确定如何将 TileBase 转换为 TileData 以便从中检索位置。

// This code block was gathered from another answer. 
BoundsInt bounds = tilemap.cellBounds;
TileBase[] allTiles = tilemap.GetTilesBlock(bounds);

for (int x = 0; x < bounds.size.x; x++) {
    for (int y = 0; y < bounds.size.y; y++) {
        TileBase tile = allTiles[x + y * bounds.size.x];

        if (tile != null) {
            Debug.Log("x:" + x + " y:" + y + " tile:" + tile.name);
            // How do I get the transform.position of the tile from
            // the x, y indexes?
        } else {
            Debug.Log("x:" + x + " y:" + y + " tile: (null)");
        }
    }
}

见我上面的评论:

"我如何从 x, y 获取图块的 transform.position 索引?”

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    使用此函数从 Tile 中获取 TileData:

    https://docs.unity3d.com/ScriptReference/Tilemaps.Tile.GetTileData.html

    如果您从 TileBase 派生了自己的类,请覆盖此函数并返回 TileData:

    https://docs.unity3d.com/ScriptReference/Tilemaps.TileBase.GetTileData.html

    【讨论】:

      猜你喜欢
      • 2018-10-24
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      相关资源
      最近更新 更多