Texture Flatten Script issues...

EDIT: Code is functional! Feel free to use this.

Basically, this script’s function, Flatten( Texture2D[ ] ); will pack a set of textures into one, single texture. Each texture used MUST be read/write enabled, and is based on the alpha of each one.

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

public class TextureCombineUtility {
	
	//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,TextureFormat.ARGB32,true);
		
		
		//Resize the new texture to match the largest texture.
		foreach(Texture2D resizeTex in combines) {
			if(resizeTex.width > newTexture.width) {
				newTexture.Resize(resizeTex.width,newTexture.height);
			}
			if(resizeTex.height > newTexture.height) {
				newTexture.Resize(newTexture.width,resizeTex.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.ARGB32,true);
			addTex.SetPixels(combines[i].GetPixels(0),0);
			addTex.Resize(xx,yy,TextureFormat.ARGB32,true);
			
			
			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);
					newTexture.SetPixel(x,y,mainPixel);
					
					++x;
				}
				x=0;
				++y;
			}
			y=0;
			++i;
		}
		
		newTexture.Apply();
		
		return newTexture;
	}
	
}

Re-Update: Code is now functional. Feel free to use it.

ok it might be me here, when i run this code i get a error

Array size must be at least width*height
UnityEngine.Texture2D:SetPixels(Color[ ], Int32)
TextureCombineUtility:Flatten(Texture2D[ ]) (at Assets/scripts/utilites/TextureCombineUtility.cs:40)

exact same error for me :frowning: