zoukankan      html  css  js  c++  java
  • Unity5系列资源管理AssetBundle——更新实现

    前面我们研究了AssetBundle的打包与加载,现在我们来了解下如何在项目中根据版本号更新内容。

    最最重要的一点,细心的朋友应该看到了在加载AssetBundle的MrcAssetManager类中,我们使用的WWW加载对象可以使用WWW.LoadFromCacheOrDownload方法,其中第一个参数是资源的url,第二个参数则是我们的要加载的版本号,我们将通过这个版本号决定是不是要重新从服务器上下载。
    因此,我们需要在本地和服务器上分别建立一个版本配置文件,里面包含了版本号、资源大小、更新时间、内容等一系列信息,至于存储方式可以是XML、JSON等合适你的格式,这里我用XML讲解。
    我们可以建立版本控制类VersionManager,在游戏开始时,读取本地的版本号并下载服务器的版本信息。
    读取本地版本信息:

     1 private int GetLocalVersion()
     2 {
     3     Debug.Log(path);
     4     if (!File.Exists(path))
     5     {
     6         XmlDocument doc = new XmlDocument();
     7         XmlElement version = doc.CreateElement("version");
     8         version.InnerText = "1";
     9         doc.AppendChild(version);
    10         doc.Save(path);
    11  
    12         Debug.Log("created xmlVersion successfully!");
    13     }
    14  
    15     XmlDocument docRead = new XmlDocument();
    16     docRead.Load(path);
    17     XmlElement versionRead = docRead.SelectSingleNode("version") as XmlElement;
    18     return int.Parse(versionRead.InnerText);
    19 }

    下载读取服务器的版本信息,如果不一致更新本地资源和版本信息:

     1 private void CheckVersion()
     2     {
     3         string url = "http://.../UnityFiles/AssetBundlesForBlog/LOUnityAssetversion.xml";
     4         StartCoroutine(GetServerVersion(url));
     5     }
     6  
     7     private IEnumerator GetServerVersion(string url)
     8     {
     9         WWW www = new WWW(url);
    10         yield return www;
    11  
    12         XmlDocument doc = new XmlDocument();
    13         doc.InnerXml = www.text;
    14          
    15         XmlElement version = doc.SelectSingleNode("version") as XmlElement;
    16  
    17         ServerVersion = int.Parse(version.InnerText);
    18  
    19         LocalVersion = GetLocalVersion();
    20         if (LocalVersion < ServerVersion)
    21         {
    22             Debug.Log("need update");
    23             //更新本地版本信息
    24             OverrideLocalVersion();
    25         }
    26  
    27         TestScript.DefaultTest.StartLoad();
    28     }
    29  
    30     private void OverrideLocalVersion()
    31     {
    32         XmlDocument docWrite = new XmlDocument();
    33         docWrite.Load(path);
    34         XmlElement versionWrite = docWrite.SelectSingleNode("version") as XmlElement;
    35         versionWrite.InnerText = ServerVersion.ToString();
    36         docWrite.Save(path);
    37     }

    最后,将AssetBundle管理类MrCAssetManager中WWW对象的版本号改为使用我们的当前版本号,就可以啦。这样,当我们更新了服务器上的AssetBundle资源后,只要修改下服务器上的版本号,客户端就会因为对比版本不一致而重新下载,而如果一致,就会使用存在本地的AssetBundle资源而不重新下载。

  • 相关阅读:
    POJ 2251 Dungeon Master(BFS)
    POJ 1321 棋盘问题 (DFS + 回溯)
    POJ 3009 Curling 2.0(DFS + 模拟)
    Codeforces 702D Road to Post Office(模拟 + 公式推导)
    Codeforces 702A Maximum Increase(dp)
    Codeforces 702C Cellular Network(二分)
    Codeforces 702B Powers of Two
    POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)
    POJ 2488 A Knight's Journey (回溯法 | DFS)
    POJ1094 Sorting It All Out (拓扑排序)
  • 原文地址:https://www.cnblogs.com/zhenlong/p/4859377.html
Copyright © 2011-2022 走看看