unity图集刷新工具

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

public class UITools 
{

    #region 刷新图集内容

    [MenuItem("UGUI工具/刷新图集内容")]
    static void AutoRefreshSpriteAtlas()
    {
        List checkPath = new List();
        checkPath.Add("/AssetsPach/UIAltasOrginAssets");
        string ignorePath = "UIAtlas/Common/Icon";

        Debug.Log(Application.dataPath);

        Dictionary> markList = new Dictionary>();
        for (int i = 0; i < checkPath.Count; i++)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(string.Format("{0}{1}", Application.dataPath, checkPath[i]));
            MarkDownImageInDoc(directoryInfo, ref markList, ignorePath);

            bool ret = EditorUtility.DisplayCancelableProgressBar("信息", $"正在收集信息{checkPath[i]}", i * 1.0f / checkPath.Count);
            if (ret == true)
            {
                Debug.Log("已取消处理");
                EditorUtility.ClearProgressBar();
                return;
            }
        }

        List objects = new List();
        int c = 1;
        foreach (var item in markList)
        {
            if (item.Value.Count == 0)
                continue;

            objects.Clear();
            SpriteAtlas spriteAtlas = GetOrCreateAtlas(item.Key);

            //先移除现有的
            spriteAtlas.Remove(spriteAtlas.GetPackables());

            //再添加
            foreach (var listItem in item.Value)
            {
                Sprite sprite = AssetDatabase.LoadAssetAtPath(listItem.Key);
                //if (!spriteAtlas.CanBindTo(sprite))
                objects.Add(sprite);
            }
            spriteAtlas.Add(objects.ToArray());

            bool ret = EditorUtility.DisplayCancelableProgressBar("信息", $"正在处理图集信息{spriteAtlas.name}", c * 1.0f / markList.Count);
            c += 1;

            if (ret == true)
            {
                Debug.Log("已取消处理");
                EditorUtility.ClearProgressBar();
                return;
            }
        }
        objects.Clear();

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        EditorUtility.ClearProgressBar();
    }

    static SpriteAtlas GetOrCreateAtlas(string atlasName)
    {
        string filePath = string.Format("{0}/AssetsPach/WortAsset/SpriteAtlas/{1}.spriteatlas", Application.dataPath, atlasName);
        string assetPath = string.Format("Assets/AssetsPach/WortAsset/SpriteAtlas/{0}.spriteatlas", atlasName);
        SpriteAtlas atlas = null;
        if (File.Exists(filePath))
        {
            atlas = AssetDatabase.LoadAssetAtPath(assetPath);
        }
        else
        {
            atlas = new SpriteAtlas();
            // 设置参数 可根据项目具体情况进行设置
            SpriteAtlasPackingSettings packSetting = new SpriteAtlasPackingSettings()
            {
                blockOffset = 1,
                enableRotation = false,
                enableTightPacking = false,
                padding = 4,
            };
            atlas.SetPackingSettings(packSetting);

            SpriteAtlasTextureSettings textureSetting = new SpriteAtlasTextureSettings()
            {
                readable = false,
                generateMipMaps = false,
                sRGB = true,
                filterMode = FilterMode.Bilinear,
            };
            atlas.SetTextureSettings(textureSetting);

            TextureImporterPlatformSettings platformSetting = new TextureImporterPlatformSettings()
            {
                maxTextureSize = 2048,
                format = TextureImporterFormat.Automatic,
                crunchedCompression = true,
                textureCompression = TextureImporterCompression.Compressed,
                compressionQuality = 50,
            };
            atlas.SetPlatformSettings(platformSetting);

            AssetDatabase.CreateAsset(atlas, assetPath);
        }

        return atlas;
    }

    static void MarkDownImageInDoc(DirectoryInfo docInfo, ref Dictionary> markList, string ignorePath)
    {
        if (docInfo.FullName.Replace("\\", "/").Contains(ignorePath))
            return;

        foreach (var file in docInfo.GetFiles())
        {
            string fileName = file.Name;
            string filePath = file.FullName.Replace("\\", "/");
            filePath = filePath.Replace(Application.dataPath, "Assets/");
            if (fileName.EndsWith(".png") || fileName.EndsWith(".jpg") || fileName.EndsWith(".jpeg"))
            {
                Dictionary subList;
                if (markList.TryGetValue(docInfo.Name, out subList))
                {
                    if (subList.ContainsKey(fileName))
                    {
                        Debug.LogErrorFormat("图集有重复图片 :图集名称:{0} 图片名称:{1}", docInfo.Name, filePath);
                    }
                    else
                    {
                        subList.Add(filePath, 1);
                    }
                }
                else
                {
                    subList = new Dictionary();
                    subList.Add(filePath, 1);
                    markList.Add(docInfo.Name, subList);
                }
            }
        }
        foreach (var doc in docInfo.GetDirectories())
        {
            MarkDownImageInDoc(doc, ref markList, ignorePath);
        }
    }

    #endregion

}
 
  

你可能感兴趣的:(unity,ui,图集)