【Unity编辑器】指定目标文件夹并导出图集

功能:右键目标文件夹,根据所在路径导出到目标路径,并生成对应的SpriteAtlas文件

using UnityEngine;
using UnityEditor;
using UnityEditor.U2D;
using UnityEngine.U2D;

public class GenerateAtlas : EditorWindow
{
    
    
    static string[,] pathPair = new string[,]
    {
    
    
        {
    
     "/Texture/UI/", "/SpriteAtals/" },
        {
    
     "/ArtResources/Sprites/", "/Resources/SpriteAltas/"},
    };

    [MenuItem("Assets/生成图集(选中文件夹)")]
    private static void Generate()
    {
    
    
        for(int i = 0; i < Selection.objects.Length; i++)
        {
    
    
            // 获取当前选择的文件夹路径
            string path = AssetDatabase.GetAssetPath(Selection.objects[i]);
            if (string.IsNullOrEmpty(path)) return;

            // 创建图集文件
            SpriteAtlas atlas = new SpriteAtlas();
            SpriteAtlasPackingSettings packingSettings = new SpriteAtlasPackingSettings()
            {
    
    
                blockOffset = 1,
                enableRotation = false,
                enableTightPacking = true,
                padding = 2
            };
            SpriteAtlasTextureSettings textureSettings = new SpriteAtlasTextureSettings()
            {
    
    
                anisoLevel = 1,

                filterMode = FilterMode.Bilinear,
                readable = false,
                sRGB = true
            };

            var atlasSetting = atlas.GetPlatformSettings("DefaultTexturePlatform");
            atlasSetting.maxTextureSize = 2048;

            atlas.SetPackingSettings(packingSettings);
            atlas.SetTextureSettings(textureSettings);
            atlas.SetPlatformSettings(atlasSetting);



            // 设置图集所包含的Sprite
            //string[] spriteGuids = AssetDatabase.FindAssets("t:sprite", new string[] { path });
            //Sprite[] sprites = new Sprite[spriteGuids.Length];
            //for (int i = 0; i < spriteGuids.Length; i++)
            //{
    
    
            //    string spritePath = AssetDatabase.GUIDToAssetPath(spriteGuids[i]);
            //    sprites[i] = AssetDatabase.LoadAssetAtPath<Sprite>(spritePath);
            //}
            //atlas.Add(sprites);

            //设置文件夹
            Object obj = AssetDatabase.LoadAssetAtPath(path, typeof(Object));
            atlas.Add(new[] {
    
     obj });
            path = path.Replace(Selection.objects[i].name, "");
            // 判断目标文件夹下是否已存在同名图集
            string atlasPath = $"{
      
      path}/{
      
      Selection.objects[i].name.ToLower()}.spriteatlas";

            //路径转换
            for (int j = 0; j < pathPair.GetLength(0); j++)
            {
    
    
                atlasPath = atlasPath.Replace(pathPair[j, 0], pathPair[j, 1]);
            }

            SpriteAtlas oldAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(atlasPath);
            if (oldAtlas != null)
            {
    
    
                // 已存在同名图集,删除旧图集文件并替换为新的图集文件
                AssetDatabase.DeleteAsset(atlasPath);
            }

            AssetDatabase.CreateAsset(atlas, atlasPath);
            Debug.Log($"已生成图集文件:{
      
      atlasPath}");
        }
    
    }
}

猜你喜欢

转载自blog.csdn.net/qq_33205561/article/details/129308365