簡體   English   中英

已解決:Unity Tilemap - 如何將多個 tilemap 合並到一個數組中

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

編輯:這個問題已經解決了。

我正在 Unity 中制作 2D RPG 游戲。 我需要一個房間檢測......所以我想從更多的瓷磚地圖中獲取所有瓷磚並將它們合並到腳本中的一個數組中。 我有一個問題,因為並非所有瓷磚都是進口的。 例如,它導入 11 個門而不是 30 個。

我的 C# 代碼:

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);
    }

這是我的世界圖 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 );
    }
}

我很高興有任何建議。

我終於解決了。 首先,我認為這可能是因為瓷磚地圖的位置不同。 但問題是我要扔正坐標。 瓷磚地圖也有負瓷磚。 因此,如果有人遇到同樣的問題,這里是工作代碼。 問題出在 ImportTiles() 方法中……所以我只附上了它。

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++;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM