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资源而不重新下载。

  • 相关阅读:
    【故障处理】ORA-12162: TNS:net service name is incorrectly specified (转)
    android studio 编程中用到的快捷键
    java时间格式串
    android Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine.
    linux安装vmware
    x1c 2017 安装mint18的坑——grub2
    x1c2017 8G版 win linux的取舍纠结记录
    python的try finally (还真不简单)
    kafka+docker+python
    json文件不能有注释
  • 原文地址:https://www.cnblogs.com/zhenlong/p/4859377.html
Copyright © 2011-2022 走看看