public static void ResizeTileMap(tk2dTileMap tileMap, int width, int height, int partitionSizeX, int partitionSizeY) { int w = Mathf.Clamp(width, 1, MaxWidth); int h = Mathf.Clamp(height, 1, MaxHeight); tk2dRuntime.TileMap.BuilderUtil.InitDataStore(tileMap); // copy into new tilemap Layer[] layers = new Layer[tileMap.Layers.Length]; for (int layerId = 0; layerId < tileMap.Layers.Length; ++layerId) { var srcLayer = tileMap.Layers[layerId]; layers[layerId] = new Layer(srcLayer.hash, width, height, partitionSizeX, partitionSizeY); var destLayer = layers[layerId]; if (srcLayer.IsEmpty) continue; int hcopy = Mathf.Min(tileMap.height, h); int wcopy = Mathf.Min(tileMap.width, w); for (int y = 0; y < hcopy; ++y) { for (int x = 0; x < wcopy; ++x) { destLayer.SetTile(x, y, srcLayer.GetTile(x, y)); } } destLayer.Optimize(); } // copy new colors bool copyColors = (tileMap.ColorChannel != null && !tileMap.ColorChannel.IsEmpty); ColorChannel targetColors = new ColorChannel(width, height, partitionSizeX, partitionSizeY); if (copyColors) { int hcopy = Mathf.Min(tileMap.height, h) + 1; int wcopy = Mathf.Min(tileMap.width, w) + 1; for (int y = 0; y < hcopy; ++y) { for (int x = 0; x < wcopy; ++x) { targetColors.SetColor(x, y, tileMap.ColorChannel.GetColor(x, y)); } } targetColors.Optimize(); } tileMap.ColorChannel = targetColors; tileMap.Layers = layers; tileMap.width = w; tileMap.height = h; tileMap.partitionSizeX = partitionSizeX; tileMap.partitionSizeY = partitionSizeY; tk2dRuntime.TileMap.BuilderUtil.CleanRenderData(tileMap); }
public static void ResizeTileMap(tk2dTileMap tileMap, int width, int height, int partitionSizeX, int partitionSizeY) { int w = Mathf.Clamp(width, 1, MaxWidth); int h = Mathf.Clamp(height, 1, MaxHeight); // Since this only works in edit mode, prefabs can be assumed to be saved here #if (UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2) Undo.RegisterSceneUndo("Resize tile map"); #else Undo.RegisterCompleteObjectUndo(tileMap, "Resize tile map"); #endif // Delete old layer render data foreach (Layer layer in tileMap.Layers) { layer.DestroyGameData(tileMap); if (layer.gameObject != null) { tk2dUtil.DestroyImmediate(layer.gameObject); layer.gameObject = null; } } // copy into new tilemap Layer[] layers = new Layer[tileMap.Layers.Length]; for (int layerId = 0; layerId < tileMap.Layers.Length; ++layerId) { Layer srcLayer = tileMap.Layers[layerId]; layers[layerId] = new Layer(srcLayer.hash, w, h, partitionSizeX, partitionSizeY); Layer destLayer = layers[layerId]; if (srcLayer.IsEmpty) continue; int hcopy = Mathf.Min(tileMap.height, h); int wcopy = Mathf.Min(tileMap.width, w); for (int y = 0; y < hcopy; ++y) { for (int x = 0; x < wcopy; ++x) { destLayer.SetRawTile(x, y, srcLayer.GetRawTile(x, y)); } } destLayer.Optimize(); } // copy new colors bool copyColors = (tileMap.ColorChannel != null && !tileMap.ColorChannel.IsEmpty); ColorChannel targetColors = new ColorChannel(w, h, partitionSizeX, partitionSizeY); if (copyColors) { int hcopy = Mathf.Min(tileMap.height, h) + 1; int wcopy = Mathf.Min(tileMap.width, w) + 1; for (int y = 0; y < hcopy; ++y) { for (int x = 0; x < wcopy; ++x) { targetColors.SetColor(x, y, tileMap.ColorChannel.GetColor(x, y)); } } targetColors.Optimize(); } tileMap.ColorChannel = targetColors; tileMap.Layers = layers; tileMap.width = w; tileMap.height = h; tileMap.partitionSizeX = partitionSizeX; tileMap.partitionSizeY = partitionSizeY; tileMap.ForceBuild(); }