游戏内容变更之后。一般而言不会想让玩家下载整个游戏包又一次安装,由于这样会流失大量玩家。全部游戏更新是必须的。

更新的内容包含 数据、资源、代码。

基本原理:

1、将须要更新的文件打包成AssetBundle文件,并计算各个文件的crc值。

以下代码将选择的文件分别导出为AssetBundle文件,并将每一个文件的crc值写入到crc.txt文件里,在Editor文件夹建立一个类,并复制以下代码。

能够在Project文件夹导出选择的文件。

public class ExportAssetBundles {

[MenuItem("Assets/Build AssetBundle From Selection Respective -  Track dependencies")]

static void ExportResourceRespective()

{

// Bring up save panel

string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");

string dirpath = path.Substring(0,path.LastIndexOf("/")+1);

string filename = path.Substring(path.LastIndexOf("/")+1);

if (path.Length != 0) {

#if UNITY_ANDROID

string targetDir = "Android/";

BuildTarget targetBuild = BuildTarget.Android;

#elif UNITY_IPHONE

string targetDir = "iPhone/";

BuildTarget targetBuild = BuildTarget.iPhone;

#elif UNITY_STANDALONE_WIN

string targetDir = "StandaloneWindows/";

BuildTarget targetBuild = BuildTarget.StandaloneWindows;

#endif

JSDocument.JSNode node = new JSDocument.JSNode("root");

Document.SNode[] nodes = node.putChildren("filehash",Selection.objects.Length);

for(int i=0;i<Selection.objects.Length;i++)

{

string name = Selection.objects[i].name+".unity3d";

uint crc = 0;

if(!Directory.Exists(dirpath+targetDir))

Directory.CreateDirectory(dirpath+targetDir);

BuildPipeline.BuildAssetBundle(Selection.objects[i],new Object[]{ Selection.objects[i]}, dirpath+targetDir+name,out crc,BuildAssetBundleOptions.CollectDependencies,targetBuild);

nodes[i].put("name",name);

nodes[i].put("crc",crc);

}

System.IO.File.WriteAllText(dirpath+targetDir+filename+".crc.txt",node.toJSONString());

}

}

}

2.须要一个资源更新server,将导出的AssetBundle文件和crc文件上传到资源更新server。能够用一个简单的httpserver。比如nginx。

3.client进入游戏之前。首先向更新server请求crc.txt文件。然后从本地的磁盘文件夹中查找crc.txt文件,检查须要更新的文件列表。然后从server下载须要更新的文件。这样,假设server没有更改文件,则仅仅须要下载一次。

4.最新的crc.txt文件到本地,以便下次查询。

以下代码演示 3,4 步骤,当中

Engine.Instance.server_datapath = 服务器下载地址。

Engine.Instance.local_datapath = Application.persistentDataPath+"/;

public static IEnumerator  UpdateDataFromServer(UpdateProgress up)

{

string server_datapath = Engine.Instance.server_datapath;

string local_datapath = Engine.Instance.local_datapath;

byte[] server_crc_data  = null;

Dictionary<string,long> filehash_server=new Dictionary<string, long>();

Dictionary<string,long>  filehash_local=new Dictionary<string, long>();

List<string> needUpdateFile = new List<string>();

Debug.Log("Load Server FileHash");

using(WWW www = new WWW(server_datapath+"crc.txt"))

{

yield return www;

if (!String.IsNullOrEmpty(www.error))

{

Debug.Log("Load Filehash Failed");

up(1.0f);

yield break;

}

JSDocument.JSNode node = new JSDocument.JSNode("filehash",www.text);

Document.SNode[] data = node.getChildren("filehash");

for(int i=0;i<data.Length;i++)

{

filehash_server[data[i].get("name","")] = data[i].get("crc",(long)0);

}

server_crc_data = www.bytes;

}

Debug.Log("Load Local FileHash");

//从本地载入文件MD5表。可能没有

try

{

JSDocument.JSNode node = new JSDocument.JSNode("filehash",System.IO.File.ReadAllText(local_datapath+"crc.txt"));

Document.SNode[] data = node.getChildren("filehash");

for(int i=0;i<data.Length;i++)

{

filehash_local[data[i].get("name","")] = data[i].get("crc",(long)0);

}

}

catch(Exception e)

{

Debug.Log(e.Message);

}

Debug.Log("Check FileHash");

//计算须要更新的文件

foreach(KeyValuePair<string,long> data in filehash_server)

{

//更新须要的文件

if(!filehash_local.ContainsKey(data.Key) || filehash_local[data.Key] != data.Value)

{

needUpdateFile.Add(data.Key);

}

}

Debug.Log("Update File");

//下载并存储

for(int i=0;i<needUpdateFile.Count;i++)

{

using(WWW www = new WWW(server_datapath+needUpdateFile[i]))

{

yield return www;

byte[] bytes = null;

if (!String.IsNullOrEmpty(www.error))

{

Debug.Log(www.error);

yield break;

}

else

{

bytes = www.bytes;

}

up((float)i/needUpdateFile.Count);

string path = local_datapath+ needUpdateFile[i];

Debug.Log(path);

FileStream fs = new FileStream(path,FileMode.Create);

fs.Write(bytes,0,bytes.Length);

fs.Flush();

fs.Close();

//              //保存

//              BinaryWriter writer;

//              FileInfo t =new FileInfo(local_datapath+ needUpdateFile[i]);

//                if(!t.Exists)

//              {

//                  writer = new BinaryWriter(t.Open(FileMode.OpenOrCreate));

//              }

//              else

//              {

//                  t.Delete();

//                  writer = new BinaryWriter(t.Open(FileMode.Create));

//              }

//              writer.Write(bytes);

//              writer.Close();

}

}

Debug.Log("Save FileHash");

if(needUpdateFile.Count>0)

{

//保存最新的文件MD5值表

//          FileStream fs = new FileStream(local_datapath+"crc.txt",FileMode.Create);

//          fs.Write(server_crc_data,0,server_crc_data.Length);

//          fs.Flush();

//            fs.Close();

BinaryWriter writer;

FileInfo t =new FileInfo(local_datapath+"crc.txt");

if(!t.Exists)

{

writer = new BinaryWriter(t.Open(FileMode.Create));

}

else

{

t.Delete();

writer = new BinaryWriter(t.Open(FileMode.Create));

}

writer.Write(server_crc_data);

writer.Close();

Debug.Log(local_datapath+"crc.txt");

}

}

5.载入已经更新完毕的存储在本地的AssetBundle,须要注意的是。Unity3d同样的文件同一时候仅仅能有一个AssetBundle在内存中,所以我们对于同样文件的载入做了同步。

(loadRefCount)。

static HashSet<string> loadRefCount = new HashSet<string>();

public delegate void delegateLoadFinish(GameObject go);

public static IEnumerator LoadModel(string res,delegateLoadFinish onLoadFinish)

{

GameObject resObject = Resources.Load<GameObject>("cards/"+res);

if(resObject !=null)

{

onLoadFinish((GameObject)GameObject.Instantiate(resObject));

yield break;

}

//假设包括资源。返回

while(loadRefCount.Contains(res))

{

yield return true;

}

loadRefCount.Add(res);

Debug.Log(Engine.Instance.local_datapath+res+".unity3d");

AssetBundleCreateRequest crcLocalBundle = AssetBundle.CreateFromMemory(

System.IO.File.ReadAllBytes(Engine.Instance.local_datapath+res+".unity3d"));

yield return crcLocalBundle;

{

GameObject cardObject = GameObject.Instantiate(crcLocalBundle.assetBundle.mainAsset)as GameObject;

crcLocalBundle.assetBundle.Unload(false);

onLoadFinish(cardObject);

}

loadRefCount.Remove(res);

}

unity3d 自己主动文件更新系统的更多相关文章

  1. 艺萌文件上传下载及自动更新系统(基于networkComms开源TCP通信框架)

    1.艺萌文件上传下载及自动更新系统,基于Winform技术,采用CS架构,开发工具为vs2010,.net2.0版本(可以很容易升级为3.5和4.0版本)开发语言c#. 本系统主要帮助客户学习基于TC ...

  2. 制作SD更新系统时和用mfgtool工具烧录时,文件如何替换?

    制作SD更新系统时和用mfgtool工具烧录时,文件如何替换? 答:制作SD更新系统时,请按照需求选择不同mfgimages-myd*文件夹.每个文件夹里面有一个Manifest文件, 里面规定了ub ...

  3. CentOS更改yum源与更新系统

    [1] 首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cent ...

  4. CentOS 更改yum源与更新系统

    FROM:http://www.cnblogs.com/lightnear/archive/2012/10/03/2710952.html [1] 首先备份/etc/yum.repos.d/CentO ...

  5. [转]CentOS更改yum源与更新系统

    [1] 首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cent ...

  6. 引擎设计跟踪(九.9) 文件包系统(Game Package System)

    很早之前,闪现过写文件包系统的想法, 但是觉得还没有到时候. 由于目前工作上在做android ndk开发, 所以业余时间趁热做了android的移植, 因为android ndk提供的mountab ...

  7. CentOS 7更改yum源与更新系统

    在CentOS 7下更改yum源与更新系统. [1] 首先备份/etc/yum.repos.d/CentOS-Base.repo cp /etc/yum.repos.d/CentOS-Base.rep ...

  8. MAC EI Capitan上更新系统自带SVN版本号(关闭SIP方能sudo rm)

    继昨晚之后.决定更新系统自带的svn.自带的svn版本号是1.7.看官网svn:http://www.wandisco.com/subversion/download#osx 最新版本号是1.9.13 ...

  9. expect脚本同步文件 expect脚本指定host和要同步的文件 构建文件分发系统 批量远程执行命令

    自动同步文件 #!/usr/bin/expect set " spawn rsync -av root@.txt /tmp/ expect { "yes/no" { se ...

随机推荐

  1. Set&Map区别Array

    Set&Map区别Array 在Set内部,两个NaN是相等.两个对象总是不相等的.可以用length来检测 四个操作方法: add(value):添加某个值,返回Set结构本身. delet ...

  2. Springboot2本地锁实践

    在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交…. 下面我们利用 ...

  3. Reentrant protected mode kernel using virtual 8086 mode interrupt service routines

    A method for allowing a protected mode kernel to service, in virtual 8086 mode, hardware interrupts ...

  4. ASP.NET-未解决的问题

    001.((FormsIdentity)User.Identity).Ticket.UserData 用ASP.NET后台格式化成json数据后传回去的数据有&quot这样的符号 002.HT ...

  5. windows server 2008开机自动登陆无密码,关机不必写原因

    运行secpol.sec接下来,在弹出的“本地安全策略”对话框中,依次展开左边树图到“本地策略”-“安全选项”,在右边可以找到“交互式登录 无须按 Ctrl+Alt+Del”,双击该项设置为“已启用” ...

  6. POJ 2906 数学期望

    开始时直接设了一个状态,dp[i][j]为发现i种bug,j个系统有bug的期望天数.但很错误,没能转移下去.... 看了题解,设状态dp[i][j]为已发现i种bug,j个系统有bug,到完成目标状 ...

  7. CURL库的宏定义列表

    列表CURL库一共同拥有17个函数 curl_close:关闭CURL会话 curl_copy_handle:复制一个CURL会话句柄,同一时候3复制其全部參数 curl_errno:返回最后一个错误 ...

  8. Wireshark默认不抓取本地包的解决方式

    事实上这个工具我已经用过非常多年了,还叫Ethereal的时候就在用. 今天因为实验须要,要抓一下在localhost间的包,结果发现获取不到.解决方法也非常easy,在cmd下输入: route a ...

  9. vue引入swiper vue使用swiper vue脚手架使用swiper /引入js文件/引入css文件

    vue引入swiper  vue使用swiper  vue脚手架使用swiper /引入js文件/引入css文件 ------------------------------------------- ...

  10. Ubuntu14.04下Mongodb官网安装部署步骤(图文详解)(博主推荐)

    不多说,直接上干货! 在这篇博客里,我采用了非官网的安装步骤,来进行安装.走了弯路,同时,也是不建议.因为在大数据领域和实际生产里,还是要走正规的为好. Ubuntu14.04下Mongodb(离线安 ...