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<string> checkPath = new List<string>();
        checkPath.Add("/AssetsPach/UIAltasOrginAssets");
        string ignorePath = "UIAtlas/Common/Icon";

        Debug.Log(Application.dataPath);

        Dictionary<string, Dictionary<string, int>> markList = new Dictionary<string, Dictionary<string, int>>();
        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<Object> objects = new List<Object>();
        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<Sprite>(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<SpriteAtlas>(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<string, Dictionary<string, int>> 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<string, int> 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<string, int>();
                    subList.Add(filePath, 1);
                    markList.Add(docInfo.Name, subList);
                }
            }
        }
        foreach (var doc in docInfo.GetDirectories())
        {
            MarkDownImageInDoc(doc, ref markList, ignorePath);
        }
    }

    #endregion

}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值