简体   繁体   中英

How to check for different tile with unity tilemaps?

Every example I've found only to do with the new tilemap system only looks for a tile using GetTile and then checks it against itself using "this". EG.

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

I want to be able to check for a other specific tiles instead. Such as a WallTile.asset tile with a my public class WallTile : TileBase script attached I've created. I'm really stuck on how to do this.

I've tried using

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;
}

But this doesnt seem to work.

I've also tried ScriptableObject.CreateInstance(WaterTile) which also doesn't work.

@Sandro Figo 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."

To check for the tile type at a position:

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;
}

Regarding gameobject of a tile:

Your tiles must inherit from Tile class not TileBase in order to manipulate its gameobject.

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM