Unity手遊之路手遊資源熱更新策略探討

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

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

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

  • 源碼

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

  • 參考資料

Unity3d官網文檔


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

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