Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Most performant way to check if a specific tile exists anywhere on a tilemap

Discussion in '2D' started by theforgot3n1, Nov 14, 2022.

  1. theforgot3n1

    theforgot3n1

    Joined:
    Sep 26, 2018
    Posts:
    205
    I'm using

    Code (CSharp):
    1.  
    2. BoundsInt bounds = this.tilemap.cellBounds;
    3. TileBase[] allTiles = this.tilemap.GetTilesBlock(bounds);
    4.  
    to get all tiles in a given area of a tilemap. However, it isn't the most performant...
    is there a better way to find out if a tilemap contains a given tile? For example, can I access that nice little preview that exists in the tilemap showing what tiles it has?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,333
    I can only presume you've looked at the Tilemap API in the Scripting Reference and didn't see: Tilemap.HasTile
     
  3. theforgot3n1

    theforgot3n1

    Joined:
    Sep 26, 2018
    Posts:
    205
    HasTile isn't what I'm looking for, unless I misunderstand.
    In order to use HasTile I still have to use GetTilesBlock to get all the tiles to check. I don't avoid reading the entire tilemap just to find if it has 1 tile of a specific type.

    My use case is that I have an editor script which checks if a tilemap has any occurrence of a specific tile. It needs to check that every frame because it needs to run OnGUI. Preferably I wouldn't want to do any caching, just check efficiently if a tilemap has or does not have a specific tile. Is it possible?
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,503
    theforgot3n1 likes this.
  5. theforgot3n1

    theforgot3n1

    Joined:
    Sep 26, 2018
    Posts:
    205
    This sounds like just what I'm looking for. Thank you Lo-renzo!