Search Unity

Tiles: how to tell what tile asset is at a grid space

Discussion in '2D' started by theeltea, Jan 23, 2020.

  1. theeltea

    theeltea

    Joined:
    Jul 24, 2016
    Posts:
    8
    What I know:
    • I can check if a tile exists for a particular tile with
      Tilemap.HasTile(Vector3 pos)
    • I can get a reference to a tile at a position with
      TileMap.GetTile<Tile>(Vector3 pos)
    • I can check what sprite is on that tile:
      TileMap.GetTile<Tile>(Vector3 pos).sprite
    With the above, I can determine what tile is at a position by checking what sprite is on it. By having a reference to that sprite, I can check for specific interactions I care about (for example, which of two different kinds of power-ups is on this tile).

    But checking sprites seems kind of kludge-like. Can't I just check for one of the specific tiles from my tile assets? I had hoped I could use
    Tilemap.GetTile<Tile>(Vector3 pos).GetType()
    to see what tile is at that location - but that just returns "Tile".

    Checking
    Tilemap.GetTile<Tile>(Vector3 pos).name
    gives me the name of the tile asset - but that's a string, and generally I've been taught to avoid using string comparison as it's slow/klunky.

    So: given a list of tile assets, how can I check if they exist at a particular place in a tilemap? Is there a better way?
     
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Comparing the Tile asset references will work, such as determining if the Tile asset at the position on the Tilemap is in your list of Tile assets:

    Code (CSharp):
    1.  
    2. var tilemapTile = tilemap.GetTile(pos);
    3. int i = 0;
    4. for (; i < tileList.Count; ++i)
    5. {
    6.     if (tilemapTile == tileList[i])
    7.         break;
    8. }
    9. if (i < tileList.Count)
    10. {
    11.     // Do something with matched Tile
    12. }
    13.  
    If you know exactly which Tile asset is your power-up Tile, you could directly compare it:

    Code (CSharp):
    1.  
    2. var tilemapTile = tilemap.GetTile(pos);
    3. if (tilemapTile == powerupTile)
    4. {
    5.     // Do something with matched Tile
    6. }
    7.  
     
    theeltea likes this.
  3. Deleted User

    Deleted User

    Guest

    As long as you don't use it in Update or a thousand times i time in a script, using strings won't be a problem.
     
  4. theeltea

    theeltea

    Joined:
    Jul 24, 2016
    Posts:
    8
    Oh dang. I'm so embarrassed. I had references to the tile all along. I don't know why I didn't think of just comparing the actual Tile returned by
    TileMap.GetTile<Tile>(Vector3 pos)
    with the tile asset (either by dragging to the inspector or loading the resource).

    At any rate, thanks much for the answer - this makes a lot more sense and greatly simplifies identifying animated tiles. I appreciate the thoughtful reply!

     
    Last edited: Jan 26, 2020
  5. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Apart from the question of slow or fast...comparing strings is prone to error, for instance, if you decide to rename your sprites and then accordingly the assets you have to find all those names also in code or the comparison will fail without telling you about it.