Unity3d热更新(三):更新资源流程

1.生成配置文件

在资源打包AssetBundle后,需要计算资源文件的MD5值,生成配置文件。下面给出一个例子:

// 获取Res文件夹下所有文件的相对路径和MD5值
string[] files = Directory.GetFiles(resPath, "*", SearchOption.AllDirectories);
StringBuilder versions = new StringBuilder();
for (int i = 0, len = files.Length; i < len; i++)
{
    string filePath = files[i];
    string extension = filePath.Substring(files[i].LastIndexOf("."));
    if (extension == ".unity3d")
    {
        string relativePath = filePath.Replace(resPath, "").Replace("\\", "/");
        string md5 = MD5File(filePath);
        versions.Append(relativePath).Append(",").Append(md5).Append("\n");
    }
}
// 生成配置文件
FileStream stream = new FileStream(resPath + "version.txt", FileMode.Create);
byte[] data = Encoding.UTF8.GetBytes(versions.ToString());
stream.Write(data, 0, data.Length);
stream.Flush();
stream.Close();
其中生成MD5值的方法如下:

public static string MD5File(string file)
{
    try
    {
        FileStream fs = new FileStream(file, 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();
    }
    catch (Exception ex)
    {
        throw new Exception("md5file() fail, error:" + ex.Message);
    }
}

2.版本比较

先加载本地的version.txt,将结果缓存起来。下载服务器的version.txt,与本地的version进行比较,筛选出需要更新和新增的资源。

3.下载资源

依次下载更新的资源,如果本地已经有旧资源,则替换之,否则就新建保存起来。

4.更新本地版本配置文件version.txt

用服务器的version.txt替换掉本地的version.txt。这样做是为了确保下次启动的时候,不会再重复更新了。


关于上述的流程,下面有个小例子。这里没有用到web服务器,而是将本地的另外一个文件夹作为资源服务器目录。这里的目录只针对windows下的版本进行测试。如果要在手机平台上,需要记得更新相关的路径。

using UnityEngine;  
using System.Collections;  
using System.Collections.Generic;  
using System.Text;  
using System.IO;  
  
public class ResUpdate : MonoBehaviour  
{  
    public static readonly string VERSION_FILE = "version.txt";  
    public static readonly string LOCAL_RES_URL = "file://" + Application.dataPath + "/Res/";  
    public static readonly string SERVER_RES_URL = "file:///C:/Res/";  
    public static readonly string LOCAL_RES_PATH = Application.dataPath + "/Res/";  
  
    private Dictionary LocalResVersion;  
    private Dictionary ServerResVersion;  
    private List NeedDownFiles;  
    private bool NeedUpdateLocalVersionFile = false;  
  
    void Start()  
    {  
        //初始化  
        LocalResVersion = new Dictionary();  
        ServerResVersion = new Dictionary();  
        NeedDownFiles = new List();  
  
        //加载本地version配置  
        StartCoroutine(DownLoad(LOCAL_RES_URL + VERSION_FILE, delegate(WWW localVersion)  
        {  
            //保存本地的version  
            ParseVersionFile(localVersion.text, LocalResVersion);  
            //加载服务端version配置  
            StartCoroutine(this.DownLoad(SERVER_RES_URL + VERSION_FILE, delegate(WWW serverVersion)  
            {  
                //保存服务端version  
                ParseVersionFile(serverVersion.text, ServerResVersion);  
                //计算出需要重新加载的资源  
                CompareVersion();  
                //加载需要更新的资源  
                DownLoadRes();  
            }));  
  
        }));  
    }  
  
    //依次加载需要更新的资源  
    private void DownLoadRes()  
    {  
        if (NeedDownFiles.Count == 0)  
        {  
            UpdateLocalVersionFile();  
            return;  
        }  
  
        string file = NeedDownFiles[0];  
        NeedDownFiles.RemoveAt(0);  
  
        StartCoroutine(this.DownLoad(SERVER_RES_URL + file, delegate(WWW w)  
        {  
            //将下载的资源替换本地就的资源  
            ReplaceLocalRes(file, w.bytes);  
            DownLoadRes();  
        }));  
    }  
  
    private void ReplaceLocalRes(string fileName, byte[] data)  
    {  
        string filePath = LOCAL_RES_PATH + fileName;  
        FileStream stream = new FileStream(LOCAL_RES_PATH + fileName, FileMode.Create);  
        stream.Write(data, 0, data.Length);  
        stream.Flush();  
        stream.Close();  
    }  
  
    //显示资源  
    private IEnumerator Show()  
    {  
        WWW asset = new WWW(LOCAL_RES_URL + "cube.assetbundle");  
        yield return asset;  
        AssetBundle bundle = asset.assetBundle;  
        Instantiate(bundle.Load("Cube"));  
        bundle.Unload(false);  
    }  
  
    //更新本地的version配置  
    private void UpdateLocalVersionFile()  
    {  
        if (NeedUpdateLocalVersionFile)  
        {  
            StringBuilder versions = new StringBuilder();  
            foreach (var item in ServerResVersion)  
            {  
                versions.Append(item.Key).Append(",").Append(item.Value).Append("\n");  
            }  
  
            FileStream stream = new FileStream(LOCAL_RES_PATH + VERSION_FILE, FileMode.Create);  
            byte[] data = Encoding.UTF8.GetBytes(versions.ToString());  
            stream.Write(data, 0, data.Length);  
            stream.Flush();  
            stream.Close();  
        }  
        //加载显示对象  
        StartCoroutine(Show());  
    }  
  
    private void CompareVersion()  
    {  
        foreach (var version in ServerResVersion)  
        {  
            string fileName = version.Key;  
            string serverMd5 = version.Value;  
            //新增的资源  
            if (!LocalResVersion.ContainsKey(fileName))  
            {  
                NeedDownFiles.Add(fileName);  
            }  
            else  
            {  
                //需要替换的资源  
                string localMd5;  
                LocalResVersion.TryGetValue(fileName, out localMd5);  
                if (!serverMd5.Equals(localMd5))  
                {  
                    NeedDownFiles.Add(fileName);  
                }  
            }  
        }  
        //本次有更新,同时更新本地的version.txt  
        NeedUpdateLocalVersionFile = NeedDownFiles.Count > 0;  
    }  
  
    private void ParseVersionFile(string content, Dictionary dict)  
    {  
        if (content == null || content.Length == 0)  
        {  
            return;  
        }  
        string[] items = content.Split(new char[] { '\n' });  
        foreach (string item in items)  
        {  
            string[] info = item.Split(new char[] { ',' });  
            if (info != null && info.Length == 2)  
            {  
                dict.Add(info[0], info[1]);  
            }  
        }  
  
    }  
  
    private IEnumerator DownLoad(string url, HandleFinishDownload finishFun)  
    {  
        WWW www = new WWW(url);  
        yield return www;  
        if (finishFun != null)  
        {  
            finishFun(www);  
        }  
        www.Dispose();  
    }  
  
    public delegate void HandleFinishDownload(WWW www);  
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值