简体   繁体   中英

SOLVED: Unity Tilemap - How to merge more tilemaps into one array

EDIT: This problem has been solved.

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.

I've finally solved it. Firstly I thought it could be because of different positions of the tilemaps. But the problem was I was going just threw positive coordinates. Tilemaps have tiles at negative too. So if here was someone with the same problem, here is the working code. The problem was in the method ImportTiles()...so I attached just it.

public void ImportTiles() {
    ClonedTile clonedTile;
    int w_y = 0; // X coordinate in worldGraph
    for(int y = -height/2; y<height/2; y++ ) {
        int w_x = 0; // Y coordinate in worldGraph

        for (int x = -width/2; x<width/2; x++ ) {
            Vector3Int pos = new Vector3Int( x, y, 0 );

            TileBase tile = floor.GetTile(pos);

            if( tile != null ) {
                clonedTile = new ClonedTile( w_x, w_y, TileType.Floor, false );
                tiles[w_x, w_y] = clonedTile;
                originalTiles.Add( clonedTile, tile );
            }

            tile = walls.GetTile( pos );

            if (tile != null ) {
                clonedTile = new ClonedTile( w_x, w_y, TileType.Wall, false );
                tiles[w_x, w_y] = clonedTile;
                originalTiles.Add( clonedTile, tile );
            }

            tile = door.GetTile( pos );
            if(tile!= null ) {
                clonedTile = new ClonedTile( w_x, w_y, TileType.Door, false );
                tiles[w_x, w_y] = clonedTile;
                originalTiles.Add( clonedTile, tile );
            }
            w_x++;
        }
        w_y++;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM