Hi All,
Beating my head against the wall with this problem. I have a tilemap that I populate and in some case I overlay tiles (terrain, furniture, item, actors, etc) and I want a good way to access all tiles in a specific x,y location regardless of the Z index.
I originally though I could take the mouse click and use a chain of:
Vector3Int tpos = tilemap.WorldToCell(worldPoint);
Bounds bounds = tilemap.GetBoundsLocal(tpos);
TileBase[] tileArray = tilemap.GetTilesBlock(bounds);
// return non null tiles in array
to get me a set of tiles that would be a lot more performant than brute force iterating through the tilemap which obviously doesn’t scale if I want to say highlight or report on items under the mouse. Also, the above doesn’t work because of the difference between Bounds and BoundsInt (and due to my inexperience I think I messed up trying to convert them).
So a couple questions:
- How do you even use GetBoundsLocal? I find almost no documentation and no examples.
- Whats the best way to get all the tiles at a specific x,y position in a Tilemap?
Thanks in advance
Is this functionality just not supported in Unity?
Hi,
-
GetBoundsLocal will give you the local-space bounds of a region of a Grid. If you wish to get the bounds of the Tilemap, you could try tilemap.cellBounds instead. Note that this is the largest bounds for all Tiles previously placed on the Tilemap. If you wish to constrain the bounds to the correct Tiles set, you can use tilemap.CompressBounds first.
-
You could try the following if you want to get all Tiles at a specific x, y position regardless of the z value:
var count = tilemap.GetTilesRangeCount(new Vector3Int(x, y, Int32.MinValue), new Vector3Int(x, y, Int32.MaxValue));
Vector3Int[] positions = new Vector3Int[count];
TileBase[] tiles = new TileBase[count];
count = tilemap.GetTilesRangeNonAlloc(new Vector3Int(x, y, Int32.MinValue), new Vector3Int(x, y, Int32.MaxValue), positions, tiles);
for (int index = 0; index < count; index++)
{
print(positions[index]);
print(tiles[index]);
}
Thanks, that does work but I am still curious about GetBoundsLocal and how to use it to solve this problem. There is no useful example I have found showing how to tie it into some use with a Tilemap.
Maybe a better question is around the best way to build a BoundsInt object by hand? I know there is a constructor for the object but you wouldn’t know it based on the documentation: Unity - Scripting API: BoundsInt
The second argument is a Vector3 for the size but how is the size applied vs the origin vector3 that is passed in? Say the origin has a z value of -4 and the size object has a z value of 10 - what is the range that is used?
I almost always use the second constructor for BoundsInt because I find it clearer.
BoundsInt b = new BoundsInt(xMin: 0, yMin: 0, zMin: -5, sizeX: 1, sizeY: 1, sizeZ: 10);
In both cases, it’ll use the range inclusive of the min value and exclusive of the min+size value.
So, if zMin = -4, and zSize = 10
Then, range = 10 numbers total, starting from zMin = -4, -3, -2, -1, 0, 1, 2, 3, 4, 5