码迷,mamicode.com
首页 > 编程语言 > 详细

获得unity 资源中的MD5值,为资源更新配置文件做准备

时间:2017-10-25 15:17:07      阅读:548      评论:0      收藏:0      [点我收藏+]

标签:base   tools   menu   最新   ==   filesyste   bundle   tor   imp   

 在前面我们说过,如何通过代码设置需要打包的资源的 AssetBundle 的 importer属性,以及简单的一键打包,那么如果资源有了更新,我们怎么得到呢?

在unity 中,编辑器会为我们每个资源都分配一个md5值,这是唯一的,如果我们资源有了变化,那么这个md5值也会有便会,这是毋庸置疑的;

这样,我们就可以得到资源的md5值,从而比对本地资源的md5值以及最新的md5值,从而知道是不是要更新资源

在unityeditor命名空间下,有这样的方法得到这个唯一值   AssetDatabase.AssetPathToGUID(path);

那么我们做个实验。我们选中一个物体,然后得到他的路径,更具路径得到MD5值

[MenuItem("Tools/获得GUID")]
    public static void GetAssetsGuid()
    {
        Object obj = Selection.activeObject;
        string path = AssetDatabase.GetAssetPath(obj);
        Debug.Log(path);
        string str=  AssetDatabase.AssetPathToGUID(path);
        Debug.Log(str);
    }

技术分享

 

从输出可以看到,我已经得到了所需要的MD5值

但是这样太麻烦了,需要一个个去手动设置,真的是哔了狗的心情都有了,我们还是和前面说道的设置importer 一样,通过IO操作,去查找,并获得

   [MenuItem("Tools/设置GUID")]
    public static void SetAssetGUID()
    {
        CheckAssetBundleDir(GetAssetBundleStringPath());
    }

    public static string GetAssetBundleStringPath()  //得到存放打包资源的文件路径
    {
        string path = Application.streamingAssetsPath;
        //Debug.Log(path);
        string[] strs = path.Split(/);
        int idx = 0;
        for (int i = 0; i < strs.Length; i++)
        {
            if (strs[i] == "Assets")
                idx = i;
        }
        // Debug.Log(idx);
        //path = strs[strs.Length - 2] + "/" + strs[strs.Length - 1];
        string str = "";
        for (int i = idx; i < strs.Length; i++)
        {
            if (i != strs.Length - 1)
                str += strs[i] + "/";
            else if (i == strs.Length - 1)
                str += strs[i];
            //Debug.Log(i);
        }
        path = str;
        return path;
        //Debug.Log(path);
    }

    public static void CheckAssetBundleDir(string path)   //是文件,继续向下
    {
        DirectoryInfo directory = new DirectoryInfo(@path);
        FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos();

        foreach (var item in fileSystemInfos)
        {
            // Debug.Log(item);
            int idx = item.ToString().LastIndexOf(@"\");
            string name = item.ToString().Substring(idx + 1);

            if (!name.Contains(".meta"))
            {
                CheckAssetBundleFileOrDirectory(item, path + "/" + name);  //item  文件系统,加相对路径
            }
        }
    }

    public static void CheckAssetBundleFileOrDirectory(FileSystemInfo fileSystemInfo, string path) //判断是文件还是文件夹
    {
        FileInfo fileInfo = fileSystemInfo as FileInfo;
        if (fileInfo != null)
        {
            //Debug.Log(path);
            Debug.Log("不为空==>>"+ GetGUID(path));
        }
        else
        {
            //Debug.Log("为空");
        }
    }

    public static string GetGUID(string path)
    {
        return AssetDatabase.AssetPathToGUID(path);
    }

这里我们同过打印,已经可以得到 父目录下的打包好的资源的 md5值了,下一步我们准备通过一层层查找,得到全部的,下 期再会

获得unity 资源中的MD5值,为资源更新配置文件做准备

标签:base   tools   menu   最新   ==   filesyste   bundle   tor   imp   

原文地址:http://www.cnblogs.com/lzy575566/p/7728598.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!