Unity2019升级2020之后图集打包问题批量处理

Unity2020版本之前图集的处理都是设置图片的PackingTag属性将多张图片打到同一张图集中,在2020版本中去掉了PackingTag属性,而是通过在Project窗口下通过Create------>2D---->Sprite Atlas 方式创建图集之后手动拖拽对应的图片到Objects for Packing列表来自定义创建图集,这种做法的优缺点姑且不论,我们这里只介绍,如果通过代码的方式批量处理之前版本的图集,使之变成新版本的图集。

本文采取的方式是通过指定图片路径,通过递归的方式一层层获取当前文件夹底下所有的图片,之后将图片加载为Texture2D的格式,获取图片的PackingTag 名称,通过字典记录起来,之后通过AssetDatabase接口动态创建一个图集资源,设置内容,保存到设置的目录中。

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.U2D;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;

public class SpriteCollection : EditorWindow
{
    /// 
    /// 需要设置的资源路径
    /// 
    public static string SpritePath = "Assets/WorldWarZ/Res/UI/UISprites";
    /// 
    /// 图集保存的路径
    /// 
    public static string AtlasPath = "Assets/WorldWarZ/Res/UI/UIAtlas";

    [MenuItem("Tools/AtlasCollection")]
    static void AtlasCollection()
    {

        //创建窗口
        Rect window = new Rect(500, 500, 500, 500);
        SpriteCollection setting = (SpriteCollection)EditorWindow.GetWindowWithRect(typeof(SpriteCollection), window, false, "AtlasCollection");
        setting.Show();
    }

    void OnGUI()
    {
        EditorGUILayout.Space();//空一行
        EditorGUILayout.LabelField("需要设置的资源路径", new GUIStyle() { alignment = TextAnchor.MiddleCenter, fontSize = 16 }, GUILayout.Height(30));
        SpritePath = EditorGUILayout.TextField(SpritePath);
        EditorGUILayout.Space();//空一行
        EditorGUILayout.LabelField("图集保存的路径", new GUIStyle() { alignment = TextAnchor.MiddleCenter, fontSize = 16 }, GUILayout.Height(30));
        AtlasPath = EditorGUILayout.TextField(AtlasPath);
        if (GUILayout.Button("开始根据图集包名称设置", GUILayout.ExpandWidth(true), GUILayout.Height(50)))
        {
            StartCollection();
        }
        EditorGUILayout.Space();//空一行
        EditorGUILayout.Space();//空一行
        if (GUILayout.Button("开始根据文件路径设置", GUILayout.ExpandWidth(true), GUILayout.Height(50)))
        {
            FileCollection();
        }
    }
    /// 
    /// 开始收集Texture2D
    /// 
    void StartCollection()
    {
        Dictionary> m_szAtlasList = GetSpriteList(true);
        SaveSprite(m_szAtlasList);
    }
    void FileCollection()
    {
        Dictionary> m_szAtlasList = GetSpriteList(false);
        SaveSprite(m_szAtlasList);
    }
    Dictionary> GetSpriteList(bool isTagName)
    {
        Dictionary> m_szAtlasList = new Dictionary>();
        SampleFile(SpritePath, m_szAtlasList, isTagName);
        return m_szAtlasList;
    }
    void SampleFile(string filePath,Dictionary> m_szAtlasList,bool IsTagName)
    {
        string[] directories = System.IO.Directory.GetDirectories(filePath);
        int i = 0, length = directories.Length;
        for (; i < length; i++)
        {
            SampleFile(directories[i], m_szAtlasList, IsTagName);
        }
        if (IsTagName)
        {
            SampleSingleFileByTagName(m_szAtlasList, filePath);
        }
        else
        {
            SampleSingleFileByFileName(m_szAtlasList, filePath);
        }

    }
    void SampleSingleFileByTagName(Dictionary> m_szAtlasList, string filePath)
    {
        string[] fileNames = System.IO.Directory.GetFiles(filePath, "*.png");
        int i=0,length = fileNames.Length;
        for (; i < length; i++)
        {
            string file = fileNames[i];
            Texture2D sprite = AssetDatabase.LoadAssetAtPath(file);
            if (sprite != null)
            {
                string tagName = GetSpriteTagName(file);
                if (tagName.Equals(""))
                    continue;
                if (m_szAtlasList.ContainsKey(tagName))
                {
                    m_szAtlasList[tagName].Add(sprite);
                }
                else
                {
                    m_szAtlasList.Add(tagName, new List() { sprite });
                }
            }
        }
    }
    void SampleSingleFileByFileName(Dictionary> m_szAtlasList, string filePath)
    {
        string[] fileNames = System.IO.Directory.GetFiles(filePath, "*.png");
        string atlasName = GetAtlasNameByFilePath(filePath);
        int i = 0, length = fileNames.Length;
        for (; i < length; i++)
        {
            string file = fileNames[i];
            Texture2D sprite = AssetDatabase.LoadAssetAtPath(file);
            if (sprite != null)
            {
                if (atlasName.Equals(""))
                    continue;
                if (m_szAtlasList.ContainsKey(atlasName))
                {
                    m_szAtlasList[atlasName].Add(sprite);
                }
                else
                {
                    m_szAtlasList.Add(atlasName, new List() { sprite });
                }
            }
        }
    }
    string GetAtlasNameByFilePath(string filePath)
    {
        string atlasName = "";
        try
        {
            int index = filePath.LastIndexOf("\\"); 
            if (index < 0)
            {
                index = filePath.LastIndexOf("/");
            }
            atlasName = filePath.Substring(index + 1, filePath.Length - index - 1);
        }
        catch (System.Exception)
        {
            Debug.LogError($"Get atlas name failed and filePath = {filePath}");
        }
        return atlasName;
    }
    /// 
    /// 得到精灵的图集名称
    /// 
    /// 
    string GetSpriteTagName(string filePath)
    {
        TextureImporter importer = AssetImporter.GetAtPath(filePath) as TextureImporter;
        if (importer == null)
        {
            return "";
        }
        return importer.spritePackingTag;
    }
    void SaveSprite(Dictionary> m_szAtlasList)
    {
        foreach (var item in m_szAtlasList)
        {
            CreateAtals(item.Key, item.Value);
        }
    }
    /// 
    /// 创建图集
    /// 
    /// 
    /// 
    void CreateAtals(string name, List sprites)
    {
        SpriteAtlas atlas = new SpriteAtlas();
        string path = string.Format(AtlasPath+"/{0}.spriteatlas", name);
        AssetDatabase.CreateAsset(atlas, path);
        atlas.name = name;
        atlas.Add(sprites.ToArray());
        SpriteAtlasPackingSettings packingSettings = GetPackSetting(atlas);
        atlas.SetPackingSettings(packingSettings);
        EditorUtility.SetDirty(atlas);
        AssetDatabase.SaveAssets();
    }
    /// 
    /// 获取图集的设置
    /// 
    /// 
    /// 
    SpriteAtlasPackingSettings GetPackSetting(SpriteAtlas atlas)
    {
        SpriteAtlasPackingSettings settings = atlas.GetPackingSettings();
        settings.enableRotation = false;
        settings.enableTightPacking = false;
        return settings;
    }
}

本篇代码可直接复制到项目中使用,编译之后在Tools标签里面找到AtlasCollection选项来打开工具。

你可能感兴趣的:(html,前端)