- Home /
Question by
Quaky · Oct 21, 2019 at 08:13 AM ·
scripting problemunity 2dtilemap
Tilemap - How to merge more tilemaps into one array
I'm making a 2D RPG game in Unity. I need a room detection... so I would like to get all tiles from more tilemaps and merge them into one array in the script. I have a problem, because not all tiles are imported. For example it imports 11 door instead of 30.
My C# code:
public class RoomController : MonoBehaviour{
public Tilemap floor;
public Tilemap walls;
public Tilemap doors;
public WorldGraph worldGraph;
public List<Room> rooms;
Queue<ClonedTile> queue;
// Start is called before the first frame update
void Start()
{
rooms = new List<Room>();
rooms.Add( new Room() );
UnityEngine.Debug.Log( "Rooms: " + rooms.Count );
worldGraph = new WorldGraph(walls.cellBounds.size.x, walls.cellBounds.size.y, walls, floor, doors, this);
}
This is my worldGraph class:
public class WorldGraph {
public ClonedTile[,] tiles;
Tilemap walls;
Tilemap floor;
Tilemap door;
Dictionary<ClonedTile, TileBase> originalTiles;
public int width;
public int height;
RoomController roomController;
public WorldGraph(int width, int height, Tilemap walls, Tilemap floor, Tilemap door, RoomController roomController ) {
tiles = new ClonedTile[width, height];
this.walls = walls;
this.floor = floor;
this.door = door;
this.width = width;
this.height = height;
this.roomController = roomController;
Debug.Log( width + " " + height );
originalTiles = new Dictionary<ClonedTile, TileBase>();
ImportTiles();
// This is the place, where I import all tiles.
public void ImportTiles() {
for(int y = 0; y<height; y++ ) {
for(int x = 0; x<width; x++ ) {
Vector3Int pos = new Vector3Int( x, y, 0 );
TileBase tile = floor.GetTile(pos);
if(tile!= null ) {
tiles[x, y] = new ClonedTile( x, y, TileType.Floor, false );
}
if(tile == null ) {
tiles[x, y] = new ClonedTile( x, y, TileType.Empty, false );
}
tile = walls.GetTile( pos );
if (tile != null ) {
tiles[x, y] = new ClonedTile( x, y, TileType.Wall, true );
}
tile = door.GetTile( pos );
if(tile!= null ) {
tiles[x, y] = new ClonedTile( x, y, TileType.Door, true );
}
}
}
int wallnumber = 0;
int floornumber = 0;
int doornumber = 0;
int emptynumber = 0;
foreach (ClonedTile t in tiles ) {
t.roomController = roomController;
roomController.GetOutsideRoom().Tiles.Add( t );
t.hasRoom = false;
switch ( t.type ) {
case TileType.Wall:
wallnumber++;
break;
case TileType.Door:
doornumber++;
break;
case TileType.Floor:
floornumber++;
break;
default:
emptynumber++;
break;
}
}
Debug.Log( "Walls: " + wallnumber + " Floor: " + floornumber + " Doors: " + doornumber + " Empty: " + emptynumber );
}
}
I would be glad for any suggestions.
Comment
Answer by ShadyProductions · Oct 21, 2019 at 08:24 AM
This is probably because you're going over the following grid size of walls.cellBounds.size.x, walls.cellBounds.size.y
But it could be that some of your doors are not contained within this grid? Perhaps you should loop over each map's cellbounds seperately?
This is just an idea, as I will need more information to help further.
Your answer
![](https://koobas.hobune.stream/wayback/20220612213654im_/https://answers.unity.com/themes/thub/images/avi.jpg)