当前位置:   article > 正文

Unity 打包图集_图集打包算法

图集打包算法

Unity打包图集

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/832810
推荐阅读
相关标签
  

闽ICP备14008679号