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); }
/// Syncs layer data and makes sure data is valid public static bool InitDataStore(tk2dTileMap tileMap) { bool dataChanged = false; int numLayers = tileMap.data.NumLayers; if (tileMap.Layers == null) { tileMap.Layers = new Layer[numLayers]; for (int i = 0; i < numLayers; ++i) tileMap.Layers[i] = new Layer(tileMap.data.Layers[i].hash, tileMap.width, tileMap.height, tileMap.partitionSizeX, tileMap.partitionSizeY); dataChanged = true; } else { // link up layer hashes Layer[] newLayers = new Layer[numLayers]; for (int i = 0; i < numLayers; ++i) { var layerInfo = tileMap.data.Layers[i]; bool found = false; // Find an existing layer with this hash for (int j = 0; j < tileMap.Layers.Length; ++j) { if (tileMap.Layers[j].hash == layerInfo.hash) { newLayers[i] = tileMap.Layers[j]; found = true; break; } } if (!found) newLayers[i] = new Layer(layerInfo.hash, tileMap.width, tileMap.height, tileMap.partitionSizeX, tileMap.partitionSizeY); } // Identify if it has changed int numActiveLayers = 0; foreach (var layer in newLayers) if (!layer.IsEmpty) numActiveLayers++; int numPreviousActiveLayers = 0; foreach (var layer in tileMap.Layers) { if (!layer.IsEmpty) numPreviousActiveLayers++; } if (numActiveLayers != numPreviousActiveLayers) dataChanged = true; tileMap.Layers = newLayers; } if (tileMap.ColorChannel == null) { tileMap.ColorChannel = new ColorChannel(tileMap.width, tileMap.height, tileMap.partitionSizeX, tileMap.partitionSizeY); } return dataChanged; }
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(); }
public static bool InitDataStore(tk2dTileMap tileMap) { bool flag = false; int numLayers = tileMap.data.NumLayers; if (tileMap.Layers == null) { tileMap.Layers = new Layer[numLayers]; for (int i = 0; i < numLayers; i++) { tileMap.Layers[i] = new Layer(tileMap.data.Layers[i].hash, tileMap.width, tileMap.height, tileMap.partitionSizeX, tileMap.partitionSizeY); } flag = true; } else { Layer[] layerArray = new Layer[numLayers]; for (int j = 0; j < numLayers; j++) { LayerInfo info = tileMap.data.Layers[j]; bool flag2 = false; for (int k = 0; k < tileMap.Layers.Length; k++) { if (tileMap.Layers[k].hash == info.hash) { layerArray[j] = tileMap.Layers[k]; flag2 = true; break; } } if (!flag2) { layerArray[j] = new Layer(info.hash, tileMap.width, tileMap.height, tileMap.partitionSizeX, tileMap.partitionSizeY); } } int num5 = 0; foreach (Layer layer in layerArray) { if (!layer.IsEmpty) { num5++; } } int num7 = 0; foreach (Layer layer2 in tileMap.Layers) { if (!layer2.IsEmpty) { num7++; } } if (num5 != num7) { flag = true; } tileMap.Layers = layerArray; } if (tileMap.ColorChannel == null) { tileMap.ColorChannel = new ColorChannel(tileMap.width, tileMap.height, tileMap.partitionSizeX, tileMap.partitionSizeY); } return flag; }
/// Syncs layer data and makes sure data is valid public static bool InitDataStore(tk2dTileMap tileMap) { bool dataChanged = false; int numLayers = tileMap.data.NumLayers; if (tileMap.Layers == null) { tileMap.Layers = new Layer[numLayers]; for (int i = 0; i < numLayers; ++i) { tileMap.Layers[i] = new Layer(tileMap.data.Layers[i].hash, tileMap.width, tileMap.height, tileMap.partitionSizeX, tileMap.partitionSizeY); } dataChanged = true; } else { // link up layer hashes Layer[] newLayers = new Layer[numLayers]; for (int i = 0; i < numLayers; ++i) { var layerInfo = tileMap.data.Layers[i]; bool found = false; // Find an existing layer with this hash for (int j = 0; j < tileMap.Layers.Length; ++j) { if (tileMap.Layers[j].hash == layerInfo.hash) { newLayers[i] = tileMap.Layers[j]; found = true; break; } } if (!found) { newLayers[i] = new Layer(layerInfo.hash, tileMap.width, tileMap.height, tileMap.partitionSizeX, tileMap.partitionSizeY); } } // Identify if it has changed int numActiveLayers = 0; foreach (var layer in newLayers) { if (!layer.IsEmpty) { numActiveLayers++; } } int numPreviousActiveLayers = 0; foreach (var layer in tileMap.Layers) { if (!layer.IsEmpty) { numPreviousActiveLayers++; } } if (numActiveLayers != numPreviousActiveLayers) { dataChanged = true; } tileMap.Layers = newLayers; } if (tileMap.ColorChannel == null) { tileMap.ColorChannel = new ColorChannel(tileMap.width, tileMap.height, tileMap.partitionSizeX, tileMap.partitionSizeY); } return(dataChanged); }
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 tk2dRuntime.TileMap.Layer[] layers = new tk2dRuntime.TileMap.Layer[tileMap.Layers.Length]; for (int layerId = 0; layerId < tileMap.Layers.Length; ++layerId) { var srcLayer = tileMap.Layers[layerId]; layers[layerId] = new tk2dRuntime.TileMap.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); }