UnityEditor开发(一) 检查重复Sprite

检查unity中重复的Sprite资源与名称重复的Sprite,打包图集使用unity2017中的新功能SpriteAtlas,SpriteAtlas与Sprite Packer的区别就不说明了,感兴趣可以百度,
   [MenuItem("Tools/Atlas/Check Atlas")]
    public static void CheckAtlas()
    {
        EditorUtility.DisplayProgressBar("Check Repetition", "Check Repetition", 0.5f);

        FileTool.q = new Queue();
        FileTool.ListFiles(new DirectoryInfo(System.Environment.CurrentDirectory + "/" + "Assets/AssetbundleHUD/"));
        string[] s = FileTool.q.ToArray();

        int filesLength = (System.Environment.CurrentDirectory + "/" + "Assets/AssetbundleHUD/HUDIF2/").Length;

        Dictionary spriteDic = new Dictionary();
        Dictionary MD5Dic = new Dictionary();

        foreach (string ss in s)
        {
            string AssetPath = ss.Substring(filesLength, ss.Length - filesLength);

            checkMD5(MD5Dic, ss);

            string[] sub = AssetPath.Split('\\');
            string SpriteName = sub[sub.Length - 1];

            if (spriteDic.ContainsKey(SpriteName))
            {
                Debug.LogError("Sprite 名称 重复,SpriteName :" + SpriteName + ",\n path: " + spriteDic[SpriteName] + ",\n path:" + ss);
            }
            else
            {
                spriteDic.Add(SpriteName, ss);
            }
        }
        EditorUtility.ClearProgressBar();

        EditorUtility.DisplayProgressBar("Check Compare", "Check Compare", 0.5f);
        CompareAtlasSpriteCount();
        EditorUtility.ClearProgressBar();
    }


    private static void CompareAtlasSpriteCount()
    {
        FileTool.q = new Queue();
        FileTool.ListFiles(new DirectoryInfo(System.Environment.CurrentDirectory + "/" + "Assets/AssetbundleResources/Atlas/"));
        string[] s = FileTool.q.ToArray();

        int filesLength = (System.Environment.CurrentDirectory + "/").Length;

        Dictionary spriteDic = new Dictionary();

        foreach (string ss in s)
        {
            string AssetPath = ss.Substring(filesLength, ss.Length - filesLength);
            string[] sub = AssetPath.Split('\\');

            string AtlasName = sub[sub.Length - 1];
            int length = ".spriteatlas".Length;
            AtlasName = AtlasName.Substring(0, AtlasName.Length - length);
            SpriteAtlas spriteAtlas = UnityEditor.AssetDatabase.LoadAssetAtPath(AssetPath, typeof(SpriteAtlas)) as SpriteAtlas;

            if (spriteAtlas == null)
                continue;

            int AtlasSpriteCount = spriteAtlas.spriteCount;


            string TexturFiles = System.Environment.CurrentDirectory + "/" + "Assets/AssetbundleHUD/HUDIF2";
            if (!ss.Contains("single"))                    //大图集不用检查吧 都是单张的
            {
                FileTool.q = new Queue();
                FileTool.ListFiles(new DirectoryInfo(TexturFiles + "/" + AtlasName));
                int TextueCount = FileTool.q.Count;
                if (TextueCount != AtlasSpriteCount)
                {
                    Debug.LogError(AtlasName + "图集中Sprite数量与资源不一致!");
                }
            }
        }
    }


    private static void checkMD5(Dictionary DIC, string path)
    {
        try
        {
            FileStream fs = new FileStream(path, FileMode.Open);
            int len = (int)fs.Length;
            byte[] data = new byte[len];
            fs.Read(data, 0, len);
            fs.Close();
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);
            string fileMD5 = "";
            foreach (byte b in result)
            {
                fileMD5 += Convert.ToString(b, 16);
            }
            if (DIC.ContainsKey(fileMD5))
            {
                Debug.LogError("Sprite 资源 重复:MD5:" + fileMD5+ ",\n path" + path + "\n path" + DIC[fileMD5]);
            }
            else
            {
                DIC.Add(fileMD5, path);
            }
        }
        catch (FileNotFoundException e)
        {
            Debug.LogError(e.Message);
        }
    }

你可能感兴趣的:(UnityEditor)