赞
踩
public class MyTexturePak { private const string intPutPath = "/Emoji/Input/"; private const string outPutPath = "Assets/Emoji/Output/"; private static readonly Vector2[] atlasSize = new Vector2[]{ new Vector2(32,32), new Vector2(64,64), new Vector2(128,128), new Vector2(256,256), new Vector2(512,512), new Vector2(1024,1024), new Vector2(2048,2048) }; private const int imageSize = 128; [MenuItem("MyTool/Texture Pak")] private static void TexturePake() { Dictionary<string, int> loadTexDic = new Dictionary<string, int>(); string[] filesStrArr = Directory.GetFiles(Application.dataPath + intPutPath, "*.png"); for (int i = 0; i < filesStrArr.Length; i++) { string[] tmpStr = filesStrArr[i].Split('/'); string fileName = tmpStr[tmpStr.Length - 1].Split('_')[0]; if (loadTexDic.ContainsKey(fileName)) { loadTexDic[fileName]++; } else { loadTexDic.Add(fileName, 1); } } if (!Directory.Exists(outPutPath)) { Directory.CreateDirectory(outPutPath); } int allF = 0; foreach (var key in loadTexDic.Keys) { allF += loadTexDic[key]; } Vector2 bigImageSize = GetSize(allF); Texture2D bigImage2D = new Texture2D((int)bigImageSize.x, (int)bigImageSize.y, TextureFormat.ARGB32, false); int x = 0; int y = 0; int ImageIndex = 0; Dictionary<string, EmojiInfo> TexPakInfoDic = new Dictionary<string, EmojiInfo>(); foreach (var key in loadTexDic.Keys) { for (int i = 0; i < loadTexDic[key]; i++) { string path = "Assets" + intPutPath + key; if (loadTexDic[key]==1) { path += ".png"; } else { path += $"_{i + 1}.png"; } Debug.Log(path); Texture2D loadSmallImage = AssetDatabase.LoadAssetAtPath<Texture2D>(path); Color[] colors = loadSmallImage.GetPixels(0); for (int j = 0; j < imageSize; j++) { for (int k = 0; k < imageSize; k++) { bigImage2D.SetPixel(x + j, y + k, colors[j + k * imageSize]); } } if (!TexPakInfoDic.ContainsKey(key)) { EmojiInfo info; info.key = $"[{ImageIndex++}]"; info.x = (x / bigImageSize.x).ToString(); info.y = (y / bigImageSize.y).ToString(); info.size = (imageSize / bigImageSize.x).ToString(); TexPakInfoDic.Add(key, info); } x += imageSize; if (x>= bigImageSize.x) { x = 0; y += imageSize; } } } byte[] buffer = bigImage2D.EncodeToPNG(); File.WriteAllBytes(outPutPath + "MyBigTexture.png", buffer); using (StreamWriter sw = new StreamWriter(outPutPath + "MyBigTextureInfo.txt")) { sw.WriteLine("Name\tKey\tFrames\tX\tY\tSize"); foreach (var key in TexPakInfoDic.Keys) { sw.WriteLine("{" + key + "}\t" + TexPakInfoDic[key].key + "\t" + loadTexDic[key] + "\t" + TexPakInfoDic[key].x + "\t" + TexPakInfoDic[key].y + "\t" + TexPakInfoDic[key].size); } } AssetDatabase.Refresh(); TextureImporter emojiTex = AssetImporter.GetAtPath(outPutPath + "MyBigTexture.png") as TextureImporter; emojiTex.filterMode = FilterMode.Point; emojiTex.mipmapEnabled = false; emojiTex.sRGBTexture = true; emojiTex.alphaSource = TextureImporterAlphaSource.FromInput; emojiTex.textureCompression = TextureImporterCompression.Uncompressed; emojiTex.SaveAndReimport(); EditorUtility.DisplayDialog("图包打包完成", "成功!", "OK"); } private static Vector2 GetSize(int allF) { long num = imageSize * imageSize * allF; for (int i = 0; i < atlasSize.Length; i++) { if (num<=atlasSize[i].x * atlasSize[i].y) { return atlasSize[i]; } } return Vector2.zero; } private struct EmojiInfo { public string key; public string x; public string y; public string size; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。