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