手遊資源熱更新策略探討

上一次我們學習了如何將資源進行打包。這次就可以用上場了,我們來探討一下手遊資源的增量更新策略。注意哦,只是資源哦。關於代碼的更新,我們稍後再來研究。理論上這個方案可以使用各種靜態資源的更新,不僅僅是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下的版本進行測試。如果要在手機平臺上,需要記得更新相關的路徑。

[csharp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5. using System.IO;  
  6.   
  7. public class ResUpdate : MonoBehaviour  
  8. {  
  9.     public static readonly string VERSION_FILE = "version.txt";  
  10.     public static readonly string LOCAL_RES_URL = "file://" + Application.dataPath + "/Res/";  
  11.     public static readonly string SERVER_RES_URL = "file:///C:/Res/";  
  12.     public static readonly string LOCAL_RES_PATH = Application.dataPath + "/Res/";  
  13.   
  14.     private Dictionary<stringstring> LocalResVersion;  
  15.     private Dictionary<stringstring> ServerResVersion;  
  16.     private List<string> NeedDownFiles;  
  17.     private bool NeedUpdateLocalVersionFile = false;  
  18.   
  19.     void Start()  
  20.     {  
  21.         //初始化  
  22.         LocalResVersion = new Dictionary<stringstring>();  
  23.         ServerResVersion = new Dictionary<stringstring>();  
  24.         NeedDownFiles = new List<string>();  
  25.   
  26.         //加載本地version配置  
  27.         StartCoroutine(DownLoad(LOCAL_RES_URL + VERSION_FILE, delegate(WWW localVersion)  
  28.         {  
  29.             //保存本地的version  
  30.             ParseVersionFile(localVersion.text, LocalResVersion);  
  31.             //加載服務端version配置  
  32.             StartCoroutine(this.DownLoad(SERVER_RES_URL + VERSION_FILE, delegate(WWW serverVersion)  
  33.             {  
  34.                 //保存服務端version  
  35.                 ParseVersionFile(serverVersion.text, ServerResVersion);  
  36.                 //計算出需要重新加載的資源  
  37.                 CompareVersion();  
  38.                 //加載需要更新的資源  
  39.                 DownLoadRes();  
  40.             }));  
  41.   
  42.         }));  
  43.     }  
  44.   
  45.     //依次加載需要更新的資源  
  46.     private void DownLoadRes()  
  47.     {  
  48.         if (NeedDownFiles.Count == 0)  
  49.         {  
  50.             UpdateLocalVersionFile();  
  51.             return;  
  52.         }  
  53.   
  54.         string file = NeedDownFiles[0];  
  55.         NeedDownFiles.RemoveAt(0);  
  56.   
  57.         StartCoroutine(this.DownLoad(SERVER_RES_URL + file, delegate(WWW w)  
  58.         {  
  59.             //將下載的資源替換本地就的資源  
  60.             ReplaceLocalRes(file, w.bytes);  
  61.             DownLoadRes();  
  62.         }));  
  63.     }  
  64.   
  65.     private void ReplaceLocalRes(string fileName, byte[] data)  
  66.     {  
  67.         string filePath = LOCAL_RES_PATH + fileName;  
  68.         FileStream stream = new FileStream(LOCAL_RES_PATH + fileName, FileMode.Create);  
  69.         stream.Write(data, 0, data.Length);  
  70.         stream.Flush();  
  71.         stream.Close();  
  72.     }  
  73.   
  74.     //顯示資源  
  75.     private IEnumerator Show()  
  76.     {  
  77.         WWW asset = new WWW(LOCAL_RES_URL + "cube.assetbundle");  
  78.         yield return asset;  
  79.         AssetBundle bundle = asset.assetBundle;  
  80.         Instantiate(bundle.Load("Cube"));  
  81.         bundle.Unload(false);  
  82.     }  
  83.   
  84.     //更新本地的version配置  
  85.     private void UpdateLocalVersionFile()  
  86.     {  
  87.         if (NeedUpdateLocalVersionFile)  
  88.         {  
  89.             StringBuilder versions = new StringBuilder();  
  90.             foreach (var item in ServerResVersion)  
  91.             {  
  92.                 versions.Append(item.Key).Append(",").Append(item.Value).Append("\n");  
  93.             }  
  94.   
  95.             FileStream stream = new FileStream(LOCAL_RES_PATH + VERSION_FILE, FileMode.Create);  
  96.             byte[] data = Encoding.UTF8.GetBytes(versions.ToString());  
  97.             stream.Write(data, 0, data.Length);  
  98.             stream.Flush();  
  99.             stream.Close();  
  100.         }  
  101.         //加載顯示對象  
  102.         StartCoroutine(Show());  
  103.     }  
  104.   
  105.     private void CompareVersion()  
  106.     {  
  107.         foreach (var version in ServerResVersion)  
  108.         {  
  109.             string fileName = version.Key;  
  110.             string serverMd5 = version.Value;  
  111.             //新增的資源  
  112.             if (!LocalResVersion.ContainsKey(fileName))  
  113.             {  
  114.                 NeedDownFiles.Add(fileName);  
  115.             }  
  116.             else  
  117.             {  
  118.                 //需要替換的資源  
  119.                 string localMd5;  
  120.                 LocalResVersion.TryGetValue(fileName, out localMd5);  
  121.                 if (!serverMd5.Equals(localMd5))  
  122.                 {  
  123.                     NeedDownFiles.Add(fileName);  
  124.                 }  
  125.             }  
  126.         }  
  127.         //本次有更新,同時更新本地的version.txt  
  128.         NeedUpdateLocalVersionFile = NeedDownFiles.Count > 0;  
  129.     }  
  130.   
  131.     private void ParseVersionFile(string content, Dictionary<stringstring> dict)  
  132.     {  
  133.         if (content == null || content.Length == 0)  
  134.         {  
  135.             return;  
  136.         }  
  137.         string[] items = content.Split(new char[] { '\n' });  
  138.         foreach (string item in items)  
  139.         {  
  140.             string[] info = item.Split(new char[] { ',' });  
  141.             if (info != null && info.Length == 2)  
  142.             {  
  143.                 dict.Add(info[0], info[1]);  
  144.             }  
  145.         }  
  146.   
  147.     }  
  148.   
  149.     private IEnumerator DownLoad(string url, HandleFinishDownload finishFun)  
  150.     {  
  151.         WWW www = new WWW(url);  
  152.         yield return www;  
  153.         if (finishFun != null)  
  154.         {  
  155.             finishFun(www);  
  156.         }  
  157.         www.Dispose();  
  158.     }  
  159.   
  160.     public delegate void HandleFinishDownload(WWW www);  
  161. }  

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

  • 源碼

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

  • 參考資料

Unity3d官網文檔


(這篇文章剛好是2014年的第一天完成的。不夠過去如何,終將過去。我們依然努力,期許能改變世界一點,不希望世界將我們改變。祝大家在新的一年夢想都實現吧:)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章