Unity手游之路<十二>手游资源热更新策略探讨

2023-04-28,,

http://blog.csdn.net/janeky/article/details/17666409

上一次我们学习了如何将资源进行打包。这次就可以用上场了,我们来探讨一下手游资源的增量更新策略。注意哦,只是资源哦。关于代码的更新,我们稍后再来研究。理论上这个方案可以使用各种静态资源的更新,不仅仅是assetbundle打包的。

(转载请注明原文地址http://blog.csdn.net/janeky/article/details/17666409)

原理

现在的手游安装有几种方式。一种是安装的时候就把程序和资源安装到本地。另外一种是只安装程序和少量的必要资源,然后在启动的时候再把缺少的资源下载完整。手游一般不建议和传统页游一样,在运行过程中加载资源,那样做会导致用户体验会比较差些。上述的两种安装模式,在更新资源上本质都是相同的。都是比较服务器资源的版本和本地资源的版本,以确定哪些资源要下载(包括需要更新的和新增的)。

实践

        1.资源打包。
资源打包之前,要先规划好资源之间的相互依赖关系。把一些共性的东西抽取出来,尽量减少不必要的耦合。一些比较好的做法有,所有物件尽可能做成Prefab,场景上的东西越少越好,“一切都是动态加载”。
        2.生成文件MD5
关于文件的MD5,这里就不详细描述了。大家可以简单理解它为一个文件的状态标记。如果文件有更改,那么它的md5一定是改变的,单纯的移动文件是不会更改的。md5验证还可以起到安全验证的作用,保证本地文件不被篡改。举个例子,我们经常从网上上下载软件时,一般都会给出一个md5值,你下载后,对比一下已下载文件的md5值,就可以知道文件有没有被篡改。在版本发布时,我们需要对所有打包好的文件计算md5值,然后保存在一个配置文件中。关于这部分的工作,我之前写过一个可视化小工具(https://github.com/kenro/File_Md5_Generator),现在分享给大家。如果大家觉得有用,记得打星哦:)
        3.版本比较
先加载本地的version.txt,将结果缓存起来。下载服务器的version.txt,与本地的version进行比较,筛选出需要更新和新增的资源
        4.下载资源
依次下载更新的资源,如果本地已经有旧资源,则替换之,否则就新建保存起来

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

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

        6.从本地加载assetbundle进行测试显示。

这里将一个模型制成Prefab,打包成assetbundle。程序从本地加载后,显示在场景中

        7.更新服务器的assetbundle,重新生成版本号文件。

        8.重复6的步骤

我们可以验证,我们的程序不用任何改动,资源已经实现了更新。场景中显示的已经是最新的模型了。

关于上述的流程,我写了一个小的演示demo。我这里没有用到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<string, string> LocalResVersion;
    private Dictionary<string, string> ServerResVersion;
    private List<string> NeedDownFiles;
    private bool NeedUpdateLocalVersionFile = false;
    void Start()
    {
    //初始化
    LocalResVersion = new Dictionary<string, string>();
    ServerResVersion = new Dictionary<string, string>();
    NeedDownFiles = new List<string>();
    //加载本地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<string, string> 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);
    }

总结

资源更新的原理,本质上都是相似的。我之前也从事过页游的开发,资源更新流程也类似。所以技术的本质是掌握思维方式,平台和语言都是永远在变的。我们最后归纳一下流程:比较服务端的资源版本和本地的资源版本,找出需要更新的资源,然后依次下载。如果大家有更好的策略,欢迎分享探讨 ken@iamcoding.com。

源码

http://pan.baidu.com/s/1mgNnR8O

参考资料

Unity3d官网文档

(这篇文章刚好是2014年的第一天完成的。不够过去如何,终将过去。我们依然努力,期许能改变世界一点,不希望世界将我们改变。祝大家在新的一年梦想都实现吧:)

Unity手游之路<十二>手游资源热更新策略探讨的相关教程结束。

《Unity手游之路<十二>手游资源热更新策略探讨.doc》

下载本文的Word格式文档,以方便收藏与打印。