public static Dictionary <string, Rect> PackTexture(Dictionary <string, Texture2D> smallTextures, string sFileName, Shader shader, EPackColorFormat eFormat = EPackColorFormat.DecideByShader) { Dictionary <string, Rect> res = new Dictionary <string, Rect>(); List <Texture2D> textureList = new List <Texture2D>(); List <string> textureIdList = new List <string>(); foreach (string stexture in smallTextures.Keys) { textureIdList.Add(stexture); textureList.Add(smallTextures[stexture]); } Texture2D bigTexture = new Texture2D(2048, 2048); Rect[] rects = bigTexture.PackTextures(textureList.ToArray(), 0, 2048); for (int i = 0; i < rects.Length; ++i) { res.Add(textureIdList[i], rects[i]); } bigTexture.Apply(true); if (bigTexture.format != TextureFormat.ARGB32 && bigTexture.format != TextureFormat.RGB24) { Texture2D newTexture = new Texture2D(bigTexture.width, bigTexture.height); newTexture.SetPixels(bigTexture.GetPixels(0), 0); bigTexture = newTexture; } if (EPackColorFormat.ForcePng == eFormat || ((null == shader || shader.renderQueue > 2400) && EPackColorFormat.ForceJpg != eFormat)) { byte[] pngData = bigTexture.EncodeToPNG(); if (pngData != null) { File.WriteAllBytes(Application.dataPath + "/" + sFileName + ".png", pngData); AssetDatabase.Refresh(); if (null != shader) { Material newMat = new Material(shader) { mainTexture = AssetDatabase.LoadAssetAtPath("Assets/" + sFileName + ".png", typeof(Texture2D)) as Texture2D }; AssetDatabase.CreateAsset(newMat, "Assets/" + sFileName + ".mat"); } } } else { byte[] pngData = bigTexture.EncodeToJPG(); if (pngData != null) { File.WriteAllBytes(Application.dataPath + "/" + sFileName + ".jpg", pngData); AssetDatabase.Refresh(); if (null != shader) { Material newMat = new Material(shader) { mainTexture = AssetDatabase.LoadAssetAtPath("Assets/" + sFileName + ".jpg", typeof(Texture2D)) as Texture2D }; AssetDatabase.CreateAsset(newMat, "Assets/" + sFileName + ".mat"); } } } AssetDatabase.Refresh(); return(res); }
public static Dictionary <string, Rect> PackTextureInDataBase(Texture2D[] smallTextures, string sFileName, Shader shader, EPackColorFormat eFormat = EPackColorFormat.DecideByShader) { Dictionary <string, Texture2D> res = new Dictionary <string, Texture2D>(); foreach (Texture2D texture in smallTextures) { string texturePath = AssetDatabase.GetAssetPath(texture); if (!res.ContainsKey(texturePath)) { string sFullPath = "file:///" + Application.dataPath + texturePath.Substring(6, texturePath.Length - 6); WWW www = new WWW(sFullPath); Texture2D t1 = AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)) as Texture2D; Debug.Assert(t1 != null, "t1 != null"); Texture2D duplic = new Texture2D(t1.width, t1.height, t1.format, t1.mipmapCount > 0); www.LoadImageIntoTexture(duplic); res.Add(texturePath, duplic); } } return(PackTexture(res, sFileName, shader, eFormat)); }