繁体   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