简体   繁体   English

已解决:Unity Tilemap - 如何将多个 tilemap 合并到一个数组中

[英]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.我正在 Unity 中制作 2D RPG 游戏。 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.例如,它导入 11 个门而不是 30 个。

My C# code:我的 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);
    }

This is my worldGraph class:这是我的世界图 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.问题出在 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