Решено: Unity Tilemap - как объединить несколько плиток в один массив - PullRequest
0 голосов
/ 20 октября 2019

РЕДАКТИРОВАТЬ: Эта проблема была решена.

Я делаю 2D RPG игру в Unity. Мне нужно обнаружение комнаты ... поэтому я хотел бы получить все плитки из большего количества плиток и объединить их в один массив в сценарии. У меня проблема, потому что не все плитки импортированы. Например, он импортирует 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);
    }

Это мой класс worldGraph:

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

Я был бы рад любомупредложения.

1 Ответ

0 голосов
/ 21 октября 2019

Я наконец-то решил это. Во-первых, я подумал, что это может быть из-за разных позиций карт. Но проблема была в том, что я собирался просто выкинуть положительные координаты. Тайлкарты тоже имеют плитки на негативе. Так что, если здесь был кто-то с той же проблемой, вот рабочий код. Проблема была в методе 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++;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...