Unity CombineTexture

public Texture2D CombineTexture(Texture2D background, Texture2D top)
    {
        int width = background.width;
        int height = background.height;
        Texture2D ret = new Texture2D(width, height);
        for(int x = 0; x < width; x++)
        {
            for(int y = 0; y < height; y++)
            {
                ret.SetPixel(x, y, background.GetPixel(x, y));
            }
        }

        int topWidth = top.width;
        int topHeight = top.height;
        int x_start = width / 2 - topWidth / 2;
        int y_start = height / 2 - topHeight / 2;

        for (int x = x_start; x < topWidth + x_start; x++)
        {
            for (int y = y_start; y < topHeight + y_start; y++)
            {
                Color bgColor = background.GetPixel(x, y);
                Color topColor = top.GetPixel(x - x_start, y - y_start);
                ret.SetPixel(x, y, Color.Lerp(bgColor, topColor, topColor.a/1f));
            }
        }
        ret.Apply();
        return ret;
    }

tip: the background and top texture need can readable.

posted @ 2017-09-13 17:01  观海云不远  阅读(421)  评论(0编辑  收藏  举报