为打包的资源设置配置文件

想要更新资源,我们就要有个配置文件,这个文件记录了资源的  ID,  版本,  下载地址

ID的话,我们可以用importer的名字,也可以用其他的,比如说GUID, 版本的话,我们可以自己定义,也可用CRC,MD5  ,一个预制体,如果没有做过更改的话,他打包出来的MD5

就不会发生变化,如果更改了的话,就会变化,我们可以 比较远端与本地的MD5值,来确定是不是要更新资源

通过获得每个资源的MD5值,设置此资源的版本,与远端进行对比,未完待续

    [MenuItem("Tools/设置配置文件")]
    public static void SetAssetConfig()
    {
        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("不为空,MD5值==>" + GetMD5(path));
        }
        else
        {
            CheckAssetBundleDir(path);
        }
    }

    public static string GetMD5(string path)
    {
        FileStream fs = new FileStream(path, FileMode.Open);
        System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] retVal = md5.ComputeHash(fs);
        fs.Close();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < retVal.Length; i++)
        {
            sb.Append(retVal[i].ToString("x2")); 
        }
        return sb.ToString();
    }

 

posted @ 2017-10-25 17:54  飞天艾特小猪  阅读(338)  评论(0编辑  收藏  举报