public static Texture2D MergeSprites(Sprite[] sprites) { PointInt Size = GetMaxSizesTextures(sprites); Texture2D mergedTexture = new Texture2D(Size.X, Size.Y); for (int y = 0; y < Size.Y; y++) { for (int x = 0; x < Size.X; x++) { Color color = new Color(0, 0, 0, 0); mergedTexture.SetPixel(x, y, color); } } mergedTexture.Apply(); Texture2D[] extendedTextures = new Texture2D[sprites.Length]; for (int it = 0; it < sprites.Length; it++) { extendedTextures[it] = ExtendTextureSize(sprites[it].texture, Size); } UnityEngine.Debug.Log("ext text: " + Size.X + ";" + Size.Y); for (int i = 0; i < sprites.Length; i++) { Texture2D mergedTextureT = CombineTextureAOverB(extendedTextures[i], mergedTexture); mergedTexture = mergedTextureT; } return(mergedTexture); }
public static PointInt GetMaxSizesTextures(Texture2D[] textures) { int maxWidth = 0; int maxHeight = 0; foreach (Texture2D t2d in textures) { maxWidth = (t2d.width > maxWidth) ? t2d.width : maxWidth; maxHeight = (t2d.height > maxHeight) ? t2d.height : maxHeight; } PointInt maxP = new PointInt(maxWidth, maxHeight); return(maxP); }
public static Texture2D ExtendTextureSize(Texture2D texture, PointInt newSize) { int oldWidth = texture.width; int oldHeight = texture.height; int newWidth = newSize.X; int newHeight = newSize.Y; int deltaW = (newWidth - oldWidth) / 2; int deltaH = (newHeight - oldHeight) / 2; Texture2D extendTexture = new Texture2D(newWidth, newHeight); for (int iH = 0; iH < newHeight; iH++) { for (int iW = 0; iW < newWidth; iW++) { Color color = (iW <deltaW || iW> newWidth - deltaW) ? new Color(0f, 0f, 0f, 0f) : texture.GetPixel(iW - deltaW, iH - deltaH); extendTexture.SetPixel(iW, iH, color); } } extendTexture.Apply(); return(extendTexture); }