首页 > 其他分享 >Sprite Packer图集导出工具

Sprite Packer图集导出工具

时间:2023-03-17 22:47:00浏览次数:57  
标签:textures Sprite Packer outDirPath tex imp var 图集

Sprite Packer只能预览,没法拿到图集图片

菜单栏 -> Window -> 2D -> Sprite Packer

 

下面的工具可以将图集导出

#if UNITY_EDITOR

using UnityEditor;
using UnityEditor.Sprites;
using UnityEngine;
using System.IO;

public class AtlasExportTool
{
    [MenuItem("MyTools/PackSpritesAndExport Select Atlas", true)]
    static bool PackSpritesAndExportSelectAtlasValidate()
    {
        return null != Selection.activeObject;
    }

    [MenuItem("MyTools/PackSpritesAndExport Select Atlas")]
    static void PackSpritesAndExportSelectAtlas()
    {
        if (null == Selection.activeObject) return;
        if (Application.isPlaying) return;

        var assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
        var imp = AssetImporter.GetAtPath(assetPath) as TextureImporter;
        if (null == imp || TextureImporterType.Sprite != imp.textureType)
        {
            Debug.Log($"所选资源不是Sprite");
            return;
        }

        if (string.IsNullOrEmpty(imp.spritePackingTag))
        {
            Debug.Log($"所选Sprite没有Packting Tag");
            return;
        }

        string outDirPath = "_AtlasTemp";
        if (!Directory.Exists(outDirPath))
        {
            Directory.CreateDirectory(outDirPath);
        }

        EditorUtility.DisplayProgressBar("RebuildAtlasCacheIfNeeded...", "RebuildAtlasCacheIfNeeded...", 0);
        Packer.RebuildAtlasCacheIfNeeded(EditorUserBuildSettings.activeBuildTarget, true, Packer.Execution.ForceRegroup);
        EditorUtility.ClearProgressBar();

        Debug.Log($"atlasName: {imp.spritePackingTag}");
        var textures = Packer.GetTexturesForAtlas(imp.spritePackingTag);
        ExportTextures(outDirPath, textures);

        var alphaTextures = Packer.GetAlphaTexturesForAtlas(imp.spritePackingTag);
        ExportTextures(outDirPath, alphaTextures);

        Debug.Log($"finish");
    }


    [MenuItem("MyTools/PackSpritesAndExport All Atlas")]
    static void PackSpritesAndExportAtlas()
    {
        if (Application.isPlaying) return;

        string outDirPath = "_AtlasTemp";
        if (!Directory.Exists(outDirPath))
        {
            Directory.CreateDirectory(outDirPath);
        }

        EditorUtility.DisplayProgressBar("RebuildAtlasCacheIfNeeded...", "RebuildAtlasCacheIfNeeded...", 0);
        Packer.RebuildAtlasCacheIfNeeded(EditorUserBuildSettings.activeBuildTarget, true, Packer.Execution.ForceRegroup);
        EditorUtility.ClearProgressBar();

        foreach (var atlasName in Packer.atlasNames)
        {
            Debug.Log($"atlasName: {atlasName}");
            var textures = Packer.GetTexturesForAtlas(atlasName);
            ExportTextures(outDirPath, textures);

            var alphaTextures = Packer.GetAlphaTexturesForAtlas(atlasName);
            ExportTextures(outDirPath, alphaTextures);
        }

        Debug.Log($"finish");
    }

    static void ExportTextures(string outDirPath, Texture[] textures)
    {
        for (int i = 0; i < textures.Length; i++)
        {
            var tex = textures[i];

            var tempRT = RenderTexture.GetTemporary(tex.width, tex.height, 0, RenderTextureFormat.ARGB32);
            Graphics.SetRenderTarget(tempRT);
            Graphics.Blit(tex, tempRT);

            var resultTex = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false);
            resultTex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
            resultTex.Apply();

            var bytes = resultTex.EncodeToPNG();

            Graphics.SetRenderTarget(null);
            RenderTexture.ReleaseTemporary(tempRT);

            //var bytes = tex.EncodeToPNG();

            var resultFilePath = "";
            if (1 == textures.Length)
                resultFilePath = Path.Combine(outDirPath, $"{tex.name}.png");
            else
                resultFilePath = Path.Combine(outDirPath, $"{tex.name}-Page{i}.png");

            File.WriteAllBytes(resultFilePath, bytes);
        }
    }

}

#endif

 

导出的图集

 

标签:textures,Sprite,Packer,outDirPath,tex,imp,var,图集
From: https://www.cnblogs.com/sailJs/p/17227356.html

相关文章

  • SpriteKit 之 SKEmitterNode
    SKEmitterNode属性介绍何时创建粒子///产生的速度,默认0varparticleBirthRate:CGFloat///产生最大数量后停止,默认是0不停止varnumParticlesToEmit:Int粒子......
  • 图集的合理使用
    图集是什么将多张图片打包到一张纹理上的技术叫图集(Atlas)。原本图片一张张送到GPU渲染,如果打包到一张纹理上,就有机会将多个物件的渲染在一次DrawCall中同时进行。(不同材质......
  • drf视图组件,视图集,路由系统,认证组件
    上周回顾#1前端开发模式-分离和混合-http通信后端都是web后端-前端:pc桌面开发,web开发,app,小程序--->http--->调用后端--->后端都是一套#2api接口......
  • drf9个视图子类,2个视图集,路由系统,认证组件
    上周内容大回顾前后端开发模式前后端混合模式 1.前端写好交给后端用后端模板语法,把数据补上去2.前后端全栈,都有一个人写前后端分离 1.前端写前端,后端就只写接口......
  • 9 个视图子类、视图集、路由系统、认证组件
    9个视图子类、视图集、路由系统、认证组件9个视图子类#两个视图基类APIViewGenericAPIView1.APIView执行流程:新的request,三大认证,全局异常重写了as_vi......
  • drf-视图集、路由系统、action装饰器
    1.9个视图扩展类1.两个视图基类:APIView、GenricAPIView2.5个视图扩展类:CreateModelMixin,UpdateModelMixin,RetrieveModelMixin,ListModelMixin,DestroyModelMixin3.9......
  • 视图集/路由自动生成
    9个视图子类在此文件中fromrest_framework.generics有九个视图子类功能分别如下9个视图子类---视图类,不需要额外继承GenericAPIView,只需要继承9个中其中某个,就会有......
  • drf视图类 视图基类、视图扩展类、视图子类、视图集以及路由类
    drf视图类视图基类、视图扩展类、视图子类、视图集两个视图基类APIView这个视图类在前文已经介绍过了,web常见5个接口--APIView的最后有提到:APIView也是继承了django......
  • 1 9 个视图子类 、2 视图集、3 路由系统、4 认证组件
    目录19个视图子类2视图集2.1通过ModelViewSet编写5个接口2.2通过ReadOnlyModelViewSet编写2个只读接口2.3ViewSetMixin源码分析2.4fromrest_framework.viewsets包......
  • 9 个视图子类、视图集、 路由系统、认证组件
    目录1.9个视图子类2视图集2.1通过ModelViewSet编写5个接口2.3ViewSetMixin源码分析2.4fromrest_framework.viewsets包下的类2.5视图层大总结3路由系统3.1自动生成......