本篇文章要介绍的是资源管理插件,主要是为了方便整理项目中的资源文件,我记得之前好像也用了这么一个插件,但是也没去找,还是自己动手写一个吧,需要什么功能就看自己的需求。
  在项目的过程中呢,已经写了一个插件来管理材质,但是跟随模型导入的图片却越来越多,也是自己跟美术沟通不到位,导致根本没办法保证图片的命名规则或者是否有重复的图片,再加上不断的删除或者更新模型,项目中一些无用的图片也堆积了不少。我尝试了自己手动整理,但是没有命名规则的图片和一些靠缩略图甚至无法分辨是不是一样的图片,这样的工作,就相当无力了。
于是呢,就完成了这么一个插件:
  1、点击Organize Resources/Editor Window打开插件,点击未使用资源按钮,这里就会罗列出Assets文件夹里所有未被引用的资源,当然当前未被引用肯定不代表他完全没用,还是得靠自己确认然后手动删除

  2、点击全部资源,他就会输出所有的文件,并可以通过按钮输出他的GUID、应用和被引用的其他Assets路径,这里主要理清一些资源的关联关系

  3、点击重复资源按钮,就可以分类显示当前重复有哪些文件,这里的文件重复判断后面有具体介绍,点击合并按钮,就可以清除掉多余的资源,如果其他本地资源引用了这个文件,合并的文件也会自动替换上去,不用担心其他文件引用会丢失

这里New Material材质引用了fish 2图片,然后我在合并界面随便点一个fish后面的合并按钮

  然后,多余的图片已经被清除了,而本来引用fish 2的材质New Material,也变成了引用fish图片,这里就是我当前需要的主要功能

上面的呢,是大概介绍了一些插件的功能,接下来就展示一下主要的功能代码:
  1、获取当前Object引用了其他哪些Object,这里在unity是有接口的,返回的就是引用了其他的Object的路径:
- string[] _OtherPaths = AssetDatabase.GetDependencies(_PathValue);  
 
  2、获取当前Object被其他那些Object引用了,说着感觉有点绕,不过功能也就是根据上面的反正写一个查找功能,获取其他Obejc的引用路径,再与当前的的Object匹配,这里的主要判断依据靠的是Object的GUID
- #region 获取其他引用Assets的路径  
 -     string[] GetUseAssetPaths(string _AssetPath)  
 -     {  
 -         List<string> _AssetPaths = new List<string>();  
 -           
 -         string _AssetGUID = AssetDatabase.AssetPathToGUID(_AssetPath);  
 -           
 -         for (int i = 0; i < _AllAssetsPaths.Count; i++)  
 -         {  
 -             if (_AllAssetsPaths[i] == _AssetPath)  
 -                 continue;  
 -   
 -             string[] _OtherPaths = AssetDatabase.GetDependencies(_AllAssetsPaths[i]);  
 -             if (_OtherPaths.Length > 1)  
 -             {  
 -                 for (int j = 0; j < _OtherPaths.Length; j++)  
 -                 {  
 -                     string _OtherGUID = AssetDatabase.AssetPathToGUID(_OtherPaths[j]);  
 -                     if (_AssetGUID == _OtherGUID)  
 -                     {  
 -                         _AssetPaths.Add(_AllAssetsPaths[i]);  
 -                     }  
 -                 }  
 -             }  
 -         }  
 -         return _AssetPaths.ToArray();  
 -     }  
 -     #endregion  
 
  3、获取相同的文件,开始使用正常的文件MD5值的验证,发现了一个问题,就是对于外部资源(从外面导入的资源,比如Texture、fbx)的验证是正常的,但是对于unity内部资源(在unity内部创建的文件,比如prefab、material)就不对了,后来读取内部资源的Text发现,他在内部有Object的名称,而Unity里的文件又不能重名,于是文件就不一样了,后来我就直接将他有名称的那行数据先删掉再来获取MD5值,才成功判断相同文件
获取相同文件
- string[] GetSameFilePaths(string _PathValue)  
 -     {  
 -         List<string> _AssetPaths = new List<string>();  
 -   
 -         string _AssetMD5 = GetFileMD5(_PathValue);  
 -           
 -         for (int i = 0; i < _AllAssetsPaths.Count; i++)  
 -         {  
 -             if (_AllAssetsPaths[i] == _PathValue)  
 -                 continue;  
 -                 if (_AssetMD5 == GetFileMD5(_AllAssetsPaths[i]))  
 -                     _AssetPaths.Add(_AllAssetsPaths[i]);  
 -   
 -         }  
 -         return _AssetPaths.ToArray();  
 -     }  
 
获取文件的MD5值
- #region 获取文件的MD5值  
 -     string GetFileMD5(string _PathValue)  
 -     {  
 -           
 -         Object _ObejctValue = AssetDatabase.LoadAssetAtPath(_PathValue);  
 -         bool _isNative =AssetDatabase.IsNativeAsset(_ObejctValue);  
 -         string _FileMD5 = "";  
 -         string _TemPath = Application.dataPath.Replace("Assets", "");  
 -   
 -         if (_isNative)  
 -         {  
 -             string _TempFileText = File.ReadAllText(_TemPath + _PathValue).Replace("m_Name: " + _ObejctValue.name,"");  
 -           
 -             System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();  
 -               
 -             byte[] fromData = System.Text.Encoding.Unicode.GetBytes(_TempFileText);  
 -               
 -             byte[] toData = md5.ComputeHash(fromData);  
 -             _FileMD5 = "";  
 -             for (int i = 0; i < toData.Length; i++)  
 -             {  
 -                 _FileMD5 += toData[i].ToString("x2");  
 -             }  
 -         }  
 -         else  
 -         {  
 -             try  
 -             {  
 -   
 -                 FileStream fs = new FileStream(_TemPath + _PathValue, FileMode.Open);  
 -   
 -                 System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();  
 -                 byte[] retVal = md5.ComputeHash(fs);  
 -                 fs.Close();  
 -                 _FileMD5 = "";  
 -                 for (int i = 0; i < retVal.Length; i++)  
 -                 {  
 -                     _FileMD5 += retVal[i].ToString("x2");  
 -                 }  
 -             }  
 -             catch (System.Exception ex)  
 -             {  
 -                 Debug.Log(ex);  
 -             }  
 -         }  
 -         return _FileMD5;  
 -     }  
 -     #endregion  
 
  4、合并时,当前文件替换掉其他被清除的文件,这里也是比较简单的,其他Object也是通过文件的GUID来引用文件,这里主要将他的GUID替换掉就可以
- #region 合并  
 -    void OnRepeatMerge(string _PathValue,List<string> _ListValue)  
 -    {  
 -        string _FixedGUID= AssetDatabase.AssetPathToGUID(_PathValue);  
 -        string _AssetsPath = Application.dataPath.Replace("Assets", "");  
 -   
 -        for (int i = 0; i < _ListValue.Count; i++)  
 -        {  
 -            if (_PathValue == _ListValue[i])  
 -                continue;  
 -            string[] _OtherPaths = GetUseAssetPaths(_ListValue[i]);  
 -              
 -            bool _isOtherNative = true;  
 -            string _OldGUI = AssetDatabase.AssetPathToGUID(_ListValue[i]);  
 -            for (int j = 0; j < _OtherPaths.Length; j++)  
 -            {  
 -                Object _OtherUseAsset = AssetDatabase.LoadAssetAtPath(_OtherPaths[j]);  
 -                if (AssetDatabase.IsNativeAsset(_OtherUseAsset))  
 -                {  
 -                    string _RealAllText = File.ReadAllText(_AssetsPath+ _OtherPaths[j]).Replace(_OldGUI,_FixedGUID);  
 -                    File.WriteAllText(_AssetsPath + _OtherPaths[j], _RealAllText);  
 -                }  
 -                else  
 -                    _isOtherNative = false;  
 -            }  
 -              
 -            if (_isOtherNative)  
 -            {  
 -                AssetDatabase.DeleteAsset(_ListValue[i]);  
 -                _ListValue.RemoveAt(i);  
 -                i--;  
 -            }  
 -        }  
 -        AssetDatabase.Refresh();  
 -        OnRepeatClick();  
 -    }  
 -    #endregion  
 
  5、最后总结,在相对较大的项目中,因为会遍历所有的文件,插件的反应速度就不那么尽如人意,想想也不会时时来整理资源,所以也暂时忽略掉了,然后,插件的功能也会在后面慢慢完善