public static void CopyTiles(List <Tilemap> sourceTilemaps, List <Tilemap> destinationTilemaps, Vector3Int offset) { sourceTilemaps = RoomTemplateUtils.GetTilemapsForCopying(sourceTilemaps); DeleteNonNullTiles(sourceTilemaps, destinationTilemaps, offset); foreach (var sourceTilemap in sourceTilemaps) { var destinationTilemap = destinationTilemaps.FirstOrDefault(x => x.name == sourceTilemap.name); if (destinationTilemap == null) { continue; } foreach (var tilemapPosition in sourceTilemap.cellBounds.allPositionsWithin) { var tile = sourceTilemap.GetTile(tilemapPosition); if (tile != null) { destinationTilemap.SetTile(tilemapPosition + offset, tile); destinationTilemap.SetTransformMatrix(tilemapPosition + offset, sourceTilemap.GetTransformMatrix(tilemapPosition)); } } } }
/// <summary> /// Copies tiles from a given room template to given destination tilemaps. /// </summary> /// <remarks> /// One important aspect of this method is how to handle already existing tiles in destination tilemaps. /// /// When deleteNonNullTiles is true, it computes all non-null positions across all layers in the room template. /// After that, it deletes all tiles on these positions in destination tilemaps. /// /// When deleteTilesInsideOutline is true, it computes all tiles inside the outline of the room template and /// deletes them from the destination tilemaps. /// So even if there is a hole inside the room template, the position is still removed. /// /// deleteNonNullTiles and deleteTilesInsideOutline can be combined together. /// </remarks> /// <param name="roomInstance">Room instance to be copied to the destination tilemaps.</param> /// <param name="destinationTilemaps">List of destination tilemaps.</param> /// <param name="deleteNonNullTiles">Whether to delete non-null tiles from destination tilemaps.</param> /// <param name="deleteTilesInsideOutline">Whether to delete all tiles insides the outline from destination tilemaps.</param> public static void CopyTiles(RoomInstance roomInstance, List <Tilemap> destinationTilemaps, bool deleteNonNullTiles, bool deleteTilesInsideOutline) { var sourceTilemaps = RoomTemplateUtils.GetTilemaps(roomInstance.RoomTemplateInstance); sourceTilemaps = RoomTemplateUtils.GetTilemapsForCopying(sourceTilemaps); var tilesToRemove = new List <Vector3Int>(); if (deleteNonNullTiles) { var tiles = GetNonNullTiles(sourceTilemaps); tilesToRemove.AddRange(tiles.Select(x => x + roomInstance.Position)); } if (deleteTilesInsideOutline) { var tiles = GetTilesInsideOutline(roomInstance, false); tilesToRemove.AddRange(tiles); } RemoveTiles(destinationTilemaps, tilesToRemove); foreach (var sourceTilemap in sourceTilemaps) { var destinationTilemap = destinationTilemaps.FirstOrDefault(x => x.name == sourceTilemap.name); if (destinationTilemap == null) { continue; } foreach (var tilemapPosition in sourceTilemap.cellBounds.allPositionsWithin) { var tile = sourceTilemap.GetTile(tilemapPosition); if (tile != null) { destinationTilemap.SetTile(tilemapPosition + roomInstance.Position, tile); destinationTilemap.SetTransformMatrix(tilemapPosition + roomInstance.Position, sourceTilemap.GetTransformMatrix(tilemapPosition)); } } } }