已解决:Unity 瓦片地图 - 如何将更多瓦片图合并到一个数组中



>编辑:这个问题已经解决了。

我正在 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);
}

这是我的世界图类:

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

我很乐意提出任何建议。

我终于解决了。首先,我认为这可能是因为瓷砖地图的位置不同。但问题是我只是抛出正坐标。瓦片地图的图块也为负数。因此,如果这里有人有同样的问题,这里是工作代码。问题出在方法导入瓷砖((...所以我只附上了它。

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

最新更新