get_used_rect()
will return the full rectangle in tile space. For example, if you have a map that is 10 tiles wide and 10 tiles tall, you'll get (0, 0, 10, 10)
. You then need to convert these into pixel space using map_to_world()
:
var tile_rect = $TileMap.get_used_rect()
var topleft = $TileMap.map_to_world(tile_rect.position)
var size = $TileMap.map_to_world(tile_rect.size)
If the TileMap is moved, you also need to add its position to place your TextureRect:
$TextureRect.rect_position = topleft + $TileMap.position
$TextureRect.rect_size = size
This works well for square tiles. It gets a bit more complicated for hex or isometric tiles.