Get notified when tile is removed

Hi,

i have added some gfx objects to my tiles to help displaying different Boxes and animation Ranges in a custom runtime editor. When i add the tile to a tilemap a hidden GameObject renders additional stuff for them. And when i delete the tile or overwrite it (this is pretty easy to check with HasTile()), i remove the hidden GameObject with the tile.

But when i use area fill several tiles could be deleted or overwritten in the process. Is there a way to notify the tile, that it will be removed from the tilemap?

Overwise i have to check every tile in the area if it is empty and i would lose the performance gain by filling an area as a block.

Greetings
Robert

Not sure if this will work for you, but you could try setting a Tilemap.tilemapTileChanged callback (Unity - Scripting API: Tilemaps.Tilemap.tilemapTileChanged). If a CellPosition does not have a Tile anymore, if has been deleted, otherwise it has been overwritten or added.

Alternatively, the hidden GameObject for the Tile could be handled using Tile.gameObject (Unity - Scripting API: Tilemaps.Tile.gameObject) The Tile will handle the lifecycle of the hidden GameObject when it is added or deleted.

Hi ChuanXin,
thx for the answer. The callback is only available in the editor and not in runtime, so i can’t use it.

The tile.gameObject is already used for prefabs i spawn when the level is played. I could double use this field to spawn a hidden gameObject only in Editor State, but i don’t think this is a very good style and may introduce other problems, but i will have a look at it.

For now i am using Unity - Scripting API: Tilemaps.TileBase.RefreshTile and inside only in Editorstate i check whether there is still a tile in the tilemap or it is a different tiletype.

TileBase tile = tilemap.GetTile( position );

if ((tile == null || !tile.Equals(this)) && hiddenObject != null)
{
    Destroy( hiddenObject );
}