天天看點

[unity3d]手遊資源熱更新政策探讨

原理

現在的手遊安裝有幾種方式。一種是安裝的時候就把程式和資源安裝到本地。另外一種是隻安裝程式和少量的必要資源,然後在啟動的時候再把缺少的資源下載下傳完整。手遊一般不建議和傳統頁遊一樣,在運作過程中加載資源,那樣做會導緻使用者體驗會比較差些。上述的兩種安裝模式,在更新資源上本質都是相同的。都是比較伺服器資源的版本和本地資源的版本,以确定哪些資源要下載下傳(包括需要更新的和新增的)。

實踐

        1.資源打包。

資源打包之前,要先規劃好資源之間的互相依賴關系。把一些共性的東西抽取出來,盡量減少不必要的耦合。一些比較好的做法有,所有物件盡可能做成Prefab,場景上的東西越少越好,“一切都是動态加載”。

        2.生成檔案MD5

        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)  

        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);  

}  

總結

資源更新的原理,本質上都是相似的。我之前也從事過頁遊的開發,資源更新流程也類似。是以技術的本質是掌握思維方式,平台和語言都是永遠在變的。我們最後歸納一下流程:比較服務端的資源版本和本地的資源版本,找出需要更新的資源,然後依次下載下傳。如果大家有更好的政策,歡迎分享探讨 [email protected]

源碼

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

參考資料

Unity3d官網文檔

本文轉蓬萊仙羽 51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366107,如需轉載請自行聯系原作者

繼續閱讀