Unity with the code multiple images into one image

In a recent write their own small projects, in the project, so I wanted to write a map editor:
1. The game map is divided a square lattice of N
2 map editor has a hierarchical function, similar to the PS layers, on each layer I can set the specified picture.
The image of each layer, which is a combined view (FIG pixel size of each non-stationary)
so that according to the above requirements, the editor creates the following:
Here Insert Picture Description
In the above editor, the panel may brush Add a picture, and then add a new level in the hierarchy panel, used to draw a new picture.
That central question is how multiple pictures into one image?
I think the method of the following methods:
1. Using the shader, but a limited number of shader merger of the picture, under the premise of defining the largest number of combined use of shader is a good choice.
2. Use C # code merge, in this project, I intend to use this approach to achieve. Without taking into account the performance of the premise, I can support the N images into one image.
3. Use PS this type of software images, the picture will need to merge. (But obviously not what I wanted)
a clear demand, it has also been resolved way, where you have to take over the need to achieve.

 public static Texture MergeTex(Texture2D[] texs)
    {
        //图片数量小于1 则 直接返回null
        if (texs.Length < 1) return null;
        //设定第一张图我们将要生成的图片尺寸
        Texture2D nTex = new Texture2D(texs[0].width,texs[0].height, TextureFormat.ARGB32,true);
        Color[] colors = new Color[nTex.width*nTex.height];
        for (int i = 0; i < texs.Length; i++)
        {
            //合并像素数量不相等的情况下 长宽的比例
            float wRate = 1, hRate = 1;
            if (texs[i].width != nTex.width)
                wRate = texs[i].width / (float)nTex.width;
            if (texs[i].height != nTex.height)
                hRate = texs[i].height / (float)nTex.height;
            for (int w = 0; w < nTex.width; w++)
            {
                for (int h = 0; h < nTex.height; h++)
                {
                    //做一个旋转,否则得到图片的朝向不正确
                    Color color = texs[i].GetPixel((int)(h * hRate),(int)((w) * wRate));
                    int index = w * nTex.width + h;
                    if (colors[index] == null) {
                        colors[index] = color;
                        continue;
                    }
                    //如果当前像素的透明为不透名 则直接进行颜色替换
                    if (color.a == 1)
                        colors[index] = color;
                    else //否则将rgb*透明度,进行颜色相加
                        colors[index] = colors[index] + new Color(color.r * color.a, color.g * color.a, color.b * color.a);
                }
            }
        }
        nTex.SetPixels(colors);
        nTex.Apply();
        return nTex;
    }

Achieve results:
Here Insert Picture Description
In a different number of pixel image merger, may cause distortion of the upper picture.
Note that, in order to be able to read the image pixel information, the Inspector will need to open the picture Read / Write Enable.
Here Insert Picture Description

Published 28 original articles · won praise 18 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_18192161/article/details/104941792