合成精灵图集——Texture2D.PackTextures()

作者:追风剑情 发布于:2017-5-17 19:06 分类:Unity3d

[MenuItem("ZWWX/Merge Selection Sprites")]
    static void MergeSprites()
    {
        UnityEngine.Object[] gos = Selection.GetFiltered(typeof(Texture2D), SelectionMode.Assets);
        Debug.Log("Selection Count="+gos.Length);
        int width = 0;
        int height = 0;
        Texture2D[] textures = new Texture2D[gos.Length];
        for (int i = 0; i < gos.Length; i++)
        {
            Texture2D tex = gos[i] as Texture2D;
            Debug.Log(tex.width);
            width += tex.width + 1;
            if (tex.height > height)
                height = tex.height;
            textures[i] = tex;
        }
        Texture2D tex2d = new Texture2D(width, height);
        tex2d.PackTextures(textures, 1);

        string savePath = Application.dataPath + "/sprite_package.png";
        try
        {
            byte[] pngData = tex2d.EncodeToPNG();
            File.WriteAllBytes(savePath, pngData);
            AssetDatabase.Refresh();
            Debug.Log("merge completed!");
            Debug.Log("Save: "+savePath);
        }
        catch (Exception ex)
        {
            Debug.Log(ex.StackTrace);
        }
    }

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号