public static Texture2D GetTextureWithAlpha(Texture2D colorSourceTexture, Texture2D alphaSourceTexture) { Texture2D tempAlpha = alphaSourceTexture; if (!(colorSourceTexture.height == alphaSourceTexture.height && colorSourceTexture.width == alphaSourceTexture.width)) { //clone and scale the alpha source texture if needed tempAlpha = GetReadableTextureFromUnreadable(alphaSourceTexture); TextureScale.Bilinear(tempAlpha, colorSourceTexture.width, colorSourceTexture.height); } var result = new Texture2D(colorSourceTexture.width, colorSourceTexture.height); var originPixels = colorSourceTexture.GetPixels(); var alphaPixels = tempAlpha.GetPixels(); for (int i = 0; i < originPixels.Length; ++i) { var col = originPixels[i]; col.a = alphaPixels[i].a; originPixels[i] = col; } result.SetPixels(originPixels); result.Apply(); //destroy the temp texture if it is created if (tempAlpha != alphaSourceTexture) { GameObject.DestroyImmediate(tempAlpha); } return(result); }
public static byte[] GetRGB24FromTexture2D(this Texture2D texture, Vector2Int size) { Color[] pixels = texture.GetPixels(); Texture2D temp = new Texture2D(texture.width, texture.height, TextureFormat.RGB24, false); temp.SetPixels(pixels); temp.Apply(); TextureScale.Bilinear(temp, size.x, size.y); return(temp.GetRawTextureData()); }