【问题标题】:How to check for different tile with unity tilemaps?如何使用统一瓷砖地图检查不同的瓷砖?
【发布时间】:2018-08-11 20:27:29
【问题描述】:

我发现的每个示例仅与新的 tilemap 系统有关,仅使用 GetTile 查找一个图块,然后使用“this”将其与自身进行检查。例如。

private bool HasOtherTile(ITilemap tilemap, Vector3Int position)
{
    return tilemap.GetTile(position) == this;
}

我希望能够改为检查其他特定图块。例如我创建的带有我的public class WallTile : TileBase 脚本的 WallTile.asset 磁贴。我真的很困惑如何做到这一点。

我尝试过使用

gameObject.AddComponent(WaterTile)

TileBase tileTest = gameObject.AddComponent(WallTile);


private bool HasRoadTile(ITilemap tilemap, Vector3Int position)
{
    TileBase tileTest = gameObject.AddComponent(WaterTile);
    return tilemap.GetTile(position) == tileTest;
}

但这似乎不起作用。

我也试过ScriptableObject.CreateInstance(WaterTile),但也没有用。

@桑德罗·菲戈 The GetTile method doesn't seem to have an extension method for gameObject it seems.

It returns "T Tile of type T placed at the cell."

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    检查某个位置的图块类型:

    public bool IsTileOfType<T>(ITilemap tilemap, Vector3Int position) where T : TileBase
    {
        TileBase targetTile = tilemap.GetTile(position);
    
        if (targetTile != null && targetTile is T)
        {
            return true;
        }
    
        return false;
    }
    

    关于棋子的游戏对象:

    您的图块必须继承自 Tile 类而不是 TileBase 才能操作其游戏对象。

    【讨论】:

      【解决方案2】:

      试试这个:

      private bool HasWallTile(ITilemap tilemap, Vector3Int position)
      {
          return tilemap.GetTile(position).gameObject.GetComponent<WallTile>() != null;
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      相关资源
      最近更新 更多