Combining textures - Flatten effect.

I'm trying to create a single, "flattened" texture from a set of others. Basically, a new texture is created, and each from the set is drawn onto it, so any transparent parts will show through to the texture behind it.

I keep getting "Can't Resize to a compressed texture format.", but I haven't been able to get rid of that error no matter what I do.

Might anyone have some suggestions?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TextureCombineUtility : MonoBehaviour {

    //Call TextureCombineUtility.Flatten([textures]) in order to create a new, single texture from multiple layers.
    public static Texture2D Flatten (Texture2D[] combines) {

        //The new texture.
        Texture2D newTexture = new Texture2D(0,0);

        //Resize the new texture to match the largest texture.
        foreach(Texture2D resizeTex in combines) {
            if(resizeTex.width > newTexture.width) {
            if(resizeTex.height > newTexture.height) {

        //Resize each layer to match the new texture, and draw the layer onto the new texture.
        int i = 0;
        int l = combines.Length;
        while(i < l) {
            int xx = newTexture.width;
            int yy = newTexture.height;

            Texture2D addTex = new Texture2D(xx,yy,TextureFormat.DXT5,true);
            addTex = combines*;*
 *int x = 0;*
 *int y = 0;*
 *while(y < yy) {*
 *while(x < xx) {*
 *Color mainPixel = newTexture.GetPixel(x,y);*
 *Color newPixel = addTex.GetPixel(x,y);*
 *mainPixel = Color.Lerp(mainPixel,newPixel,newPixel.a);*
 *return newTexture;*

Try using TextureFormat.ARGB32 instead of DXT5. Then use Compress at the end

Hello. I used your script, and made an object with a MeshRenderer and changed it’s material’s texture to the return value of your Flatten function. I added 5 textures as parameters but the result is a texture created from other 5 textures that i have in the project.

public Texture2D[] tex;

//added 5 textures that are Imported using Advance setting and compressed using ARBG32, all the same size.
void Start(){
   renderer.material.mainTexture = TextureCombineUtility.Flatten(tex);

//the result is a texture formed by other 5 textures from my project, and those are not passed as the params.