Question by
HypnoticOwl · Mar 28, 2018 at 02:50 PM ·
2dtilemap
How can I set tilemaps to have same origin and size (without loosing tiles)
I have one tilemap for walls and one for the floor. To use my A* Pathfinder I need to extract a bool[] walkmap. To do this I figured I'd just create a new bool array the size of the tilemaps and iterate through both. However both tilemaps have different origins and sizes!
My first approach was to set the origin of both tilemaps to (0,0,0), call ResizeBounds() to update them before setting the size of both tilemaps to whichever tilemap is now the biggest. Although this did work, I lost all tiles on both tilemaps!
Am I not supposed to change origin & size in a tilemap? Is there anything I'm doing wrong? Or is there a better way to extract a walkmap?
Here is my code:
void UpdateWalkmap ()
{
walls = LevelEditor.GetTilemap(Names.Walls);
floor = LevelEditor.GetTilemap(Names.Floor);
Tilemap[] tilemaps = new Tilemap[] { walls, floor };
// Unify bounds
UnifyBounds(tilemaps);
// Create Walkmap
//TODO
}
void UnifyBounds(Tilemap[] tilemaps)
{
// Compress bounds
foreach (Tilemap t in tilemaps)
{
t.CompressBounds();
}
//Unify
UnifyOrigin(tilemaps);
UnifySize(tilemaps);
}
void UnifyOrigin(Tilemap[] tilemaps)
{
// Set origin to (0,0,0)
origin = Vector3Int.zero;
// Set all origins to (0,0,0)
foreach (Tilemap t in tilemaps)
{
t.origin = origin;
t.ResizeBounds();
}
}
void UnifySize(Tilemap[] tilemaps)
{
// Set size to biggest in tilemaps
size = Vector3Int.zero;
foreach (Tilemap t in tilemaps)
{
if (size.x < t.size.x)
size.x = t.size.x;
if (size.y < t.size.y)
size.y = t.size.y;
}
// Set all origins to smalles origin
foreach (Tilemap t in tilemaps)
{
t.size = size;
t.ResizeBounds();
}
}
Comment