Exemplo n.º 1
0
        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);

            Undo.RegisterSceneUndo("Resize tile map");

            // Since this only works in edit mode, prefabs can be assumed to be saved here

            // Delete old layer render data
            foreach (Layer layer in tileMap.Layers)
            {
                if (layer.gameObject != null)
                {
                    GameObject.DestroyImmediate(layer.gameObject);
                }
            }

            // 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, width, height, 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(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;

            tileMap.ForceBuild();
        }
Exemplo n.º 2
0
        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);
        }