Unity游戏内版本更新
最近研究了一下游戏内apk包更新的方法。
ios对于应用的管理比较严格,除非热更新脚本,不太可能做到端内大版本包的更新。然而安卓端则没有此限制。因此可以做到不跳到网页或应用商店,就覆盖更新apk包。
Unity最常用的脚本语言就是C#,不做断点续传的情况下,采用C#的网络库,还是比较简单的。重点就是做好相应的异常处理。
C#用于网络访问的方法主要有两种:WebRequest和封装好的WebClient。为了将来能做更多的扩展,我采用更灵活的HttpWebRequest进行请求。为了不阻塞主线程,使用异步接口。
基本做法可参考官方文档https://msdn.microsoft.com/zh-cn/library/system.net.httpwebrequest.begingetresponse(v=vs.110).aspx
然而我们知道,Unity4.X对于多线程的支持是很弱的,不推荐使用。因此,无法在下载线程中回调相应的事件。我将回调写在主线程中,用Coroutine去轮询当前的下载状态和进度,并做相应的处理。
首先需要定义下载的状态和传入下载线程的请求状态,然后是下载的路径(可能还需要文件MD5码)以及安装路径等必要的变量,最后为了显示当前的下载进度、下载速度等,需要开启一个Coroutine或者在Update中不断查询当前下载状态,是否有异常,以及是否已经下载完毕。如果下载完毕,则校验文件,并开始安装。
using UnityEngine;
using System;
using System.Collections;
using System.Threading;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System; public class VersionUpdater : MonoBehaviour
{
public class RequestState
{
public const int BUFFER_SIZE = ;
public byte[] BufferRead;
public HttpWebRequest request;
public HttpWebResponse response;
public Stream responseStream;
} public enum DownloadState
{
DOWNLOADING,
FINISHED,
FAILED
} public delegate void ProgressCallback(long curr, long length, float rate, DownloadState state);
public ProgressCallback progressCallback; string url = "";
string installPath = "";
string apkName = "";
string errorMsg = ""; private FileStream fileStream = null;
private long length = ;
private long curr = ;
private long last = ;
private const float UpdateTime = 0.5f;
private float rate = ;
private DownloadState downState = DownloadState.DOWNLOADING; public void DownloadApkAsync(string url, string md5, string path, string name)
{
this.url = url;
this.installPath = path;
this.apkName = name;
this.errorMsg = "";
downState = DownloadState.DOWNLOADING; DownloadApkAsync();
} private void DownloadApkAsync()
{
if (string.IsNullOrEmpty(url)) return;
if (string.IsNullOrEmpty(installPath)) return;
if (string.IsNullOrEmpty(apkName)) return; string fullpath = installPath + "/" + apkName; IAsyncResult result = null;
try
{
fileStream = new FileStream(fullpath, FileMode.Create, FileAccess.Write); Uri uri = new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Method = "GET"; RequestState requestState = new RequestState();
requestState.BufferRead = new byte[RequestState.BUFFER_SIZE];
requestState.request = request; curr = ;
length = ;
rate = 0.0f;
downState = DownloadState.DOWNLOADING;
result = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(ResponeCallback), requestState);
}
catch (Exception e)
{
errorMsg = "Begin Create Exception!";
errorMsg += string.Format("Message:{0}", e.Message);
StopDownload(result);
downState = DownloadState.FAILED;
} StartCoroutine(updateProgress());
} IEnumerator updateProgress()
{
while (curr <= length)
{
yield return new WaitForSeconds(UpdateTime); rate = (curr - last) / UpdateTime;
last = curr; if (downState == DownloadState.FAILED)
{
Debug.LogError(errorMsg);
if (fileStream != null)
fileStream.Close();
if (progressCallback != null)
progressCallback( curr, length, rate, DownloadState.FAILED);
break;
} if (progressCallback != null)
progressCallback( curr, length, rate, DownloadState.DOWNLOADING); if (downState == DownloadState.FINISHED)
{
if (progressCallback != null)
progressCallback( curr, length, rate, DownloadState.FINISHED);
break;
}
}
} void StopDownload(IAsyncResult result)
{
if (result == null) return;
RequestState requestState = (RequestState)result.AsyncState;
requestState.request.Abort();
} void ResponeCallback(IAsyncResult result)
{
try
{
if (downState != DownloadState.FAILED)
{
RequestState requestState = (RequestState)result.AsyncState;
HttpWebRequest request = requestState.request;
requestState.response = (HttpWebResponse)request.EndGetResponse(result); Stream responseStream = requestState.response.GetResponseStream();
requestState.responseStream = responseStream; length = requestState.response.ContentLength; IAsyncResult readResult = responseStream.BeginRead(requestState.BufferRead, , RequestState.BUFFER_SIZE, new AsyncCallback(ReadCallback), requestState);
return;
}
}
catch (Exception e)
{
string msg = "ResponseCallback exception!\n";
msg += string.Format("Message:{0}", e.Message);
StopDownload(result);
errorMsg = msg;
downState = DownloadState.FAILED;
}
} void ReadCallback(IAsyncResult result)
{
try
{
if (downState != DownloadState.FAILED)
{
RequestState requestState = (RequestState)result.AsyncState;
Stream responseStream = requestState.responseStream;
int read = responseStream.EndRead(result);
if (read > )
{
fileStream.Write(requestState.BufferRead, , read);
fileStream.Flush();
curr += read; IAsyncResult readResult = responseStream.BeginRead(requestState.BufferRead, , RequestState.BUFFER_SIZE, new AsyncCallback(ReadCallback), requestState);
return;
}
else
{
Debug.Log("download end");
responseStream.Close();
fileStream.Close(); downState = DownloadState.FINISHED;
}
}
}
catch (Exception e)
{
string msg = "ReadCallBack exception!";
msg += string.Format("Message:{0}", e.Message);
StopDownload(result);
errorMsg = msg;
downState = DownloadState.FAILED;
}
} public void InstallApk()
{
#if UNITY_ANDROID && !UNITY_EDITOR
Debug.Log("begin install");
using (AndroidJavaObject jo = new AndroidJavaObject("com.kevonyang.androidhelper.AndroidHelper"))
{
if (jo == null)
{
WMDebug.Debug.LogError("VersionUpdater: Failed to get com.kevonyang.androidhelper.AndroidHelper");
return;
}
using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
if (jc == null)
{
WMDebug.Debug.LogError("VersionUpdater: Failed to get com.unity3d.player.UnityPlayer");
return;
}
AndroidJavaObject m_jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
if (m_jo == null)
{
WMDebug.Debug.LogError("VersionUpdater: Failed to get currentActivity");
return;
} jo.CallStatic("InstallApk", m_jo, installPath, apkName);
}
}
#endif
}
}
在下载完毕后,需要写一个java类,并在里面调用安装接口。内容很简单,只需要简单的启动一个安装的Intent就可以了,随后就会出现系统提示,是否覆盖安装。至此,游戏内的下载及安装全部完成,等待覆盖安装完毕即可从新的客户端启动。
public static void InstallApk(Context context, String path, String name) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path, name)), "application/vnd.android.package-archive");
context.startActivity(intent);
}
Unity游戏内版本更新的更多相关文章
- Unity大中华区主办 第二届Unity 游戏及应用大赛 实力派精品手游盘点
Unity是由Unity Technologies开发的一个让玩家轻松创建诸如三维视频游戏.建筑可视化.实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎.包含如今时 ...
- 2017年Unity游戏开发视频教程(入门到精通)
本文是我发布的一个Unity游戏开发的学习目录,以后我会持续发布一系列的游戏开发教程,都会更新在这个页面上,适合人群有下面的几种: 想要做独立游戏的人 想要找游戏开发相关工作的人 对游戏开发感兴趣的人 ...
- 【Unity游戏开发】浅谈Lua和C#中的闭包
一.前言 目前在Unity游戏开发中,比较流行的两种语言就是Lua和C#.通常的做法是:C#做些核心的功能和接口供Lua调用,Lua主要做些UI模块和一些业务逻辑.这样既能在保持一定的游戏运行效率的同 ...
- Unity外包团队:关于手机unity游戏开发的技术选型
技术选型 Unity引擎内置了多人联机的解决方案,涵盖了从最底层的网络数据传输,到不同玩家之间的消息发送,再到游戏大厅这样的高级功能.考虑到Unity官方提供的云服务(Internet Service ...
- C#开发Unity游戏教程之游戏对象的行为逻辑方法
C#开发Unity游戏教程之游戏对象的行为逻辑方法 游戏对象的行为逻辑——方法 方法(method),读者在第1章新建脚本时就见过了,而且在第2章对脚本做整体上的介绍时也介绍过,那么上一章呢,尽管主要 ...
- C#开发Unity游戏教程之游戏对象的属性变量
C#开发Unity游戏教程之游戏对象的属性变量 Unity游戏对象的属性——变量 通过对上一章的学习,读者应该了解到了,游戏对象上的属性与脚本中的变量,建立联系的方式就是将脚本赋予游戏对象.上一章只是 ...
- Unity即将内置骨骼动画插件Anima2D
Unity一直在寻找新的方法来帮助开发者,并为他们提供最好的工具.在此我们向大家宣布,Unity将内置流行的骨骼动画插件Anima2D,从2017年1月开始免费供所有Unity开发者使用! 同时也欢迎 ...
- Unity优化方向——优化Unity游戏中的脚本(译)
原文地址:https://unity3d.com/cn/learn/tutorials/topics/performance-optimization/optimizing-scripts-unity ...
- unity游戏在ios11上不显示泰语解决办法
最近在开发中遇到unity游戏在ios11上不显示泰语的问题,全部显示为方框内一个问号. 通过搜索发现这是Unity的一个bug,在2017.3中修复了 但升级unity风险很大,所以我采用了该文中提 ...
随机推荐
- 旺财速啃H5框架之Bootstrap(二)
突然感觉不知道写啥子,脑子里面没水了,可能是因为今晚要出去浪,哈哈~~~提前提醒大家平安夜要回家哦,圣诞节生00000000000这么多蛋....继续 上一篇的已经把bootstrap了解个大概了,接 ...
- JavaScript正则表达式,你真的知道?
一.前言 粗浅的编写正则表达式,是造成性能瓶颈的主要原因.如下: var reg1 = /(A+A+)+B/; var reg2 = /AA+B/; 上述两个正则表达式,匹配效果是一样的,但是,效率就 ...
- Java中常用集合操作
一.Map 名值对存储的. 常用派生类HashMap类 添加: put(key,value)往集合里添加数据 删除: clear()删除所有 remove(key)清除单个,根据k来找 获取: siz ...
- 网站里加入QQ在线客服
1.开启"QQ在线状态"服务 http://jingyan.baidu.com/article/b24f6c823425a586bfe5da1f.html http://www. ...
- 深入理解css3中nth-child和 nth-of-type的区别
在css3中有两个新的选择器可以选择父元素下对应的子元素,一个是:nth-child 另一个是:nth-of-type. 但是它们到底有什么区别呢? 其实区别很简单::nth-of-type为什么要叫 ...
- js 基础篇(点击事件轮播图的实现)
轮播图在以后的应用中还是比较常见的,不需要多少行代码就能实现.但是在只掌握了js基础知识的情况下,怎么来用较少的而且逻辑又简单的方法来实现呢?下面来分析下几种不同的做法: 1.利用位移的方法来实现 首 ...
- iOS 小知识点(持续更新)
1.如何通过代码设置Button title的字体大小 设置Button.titleLabel.font = [UIFont systemFontOfSize:<#(CGFloat)#> ...
- NSURLSession详解
导语 现在NSURLConnection在开发中会使用的越来越少,iOS9已经将NSURLConnection废弃,现在最低版本一般适配iOS7,所以也可以使用. NSURLConnection相对于 ...
- Ajax.BeginForm方法 参数
感谢博主 http://www.cnblogs.com/zzgblog/p/5454019.html toyoung 在Asp.Net的MVC中的语法,在Razor页面中使用,替代JQuery的Aja ...
- Linux基础介绍【第三篇】
更改SSH服务端远程登录的配置 windows服务端的默认远程管理端口是3389,管理员用户是administrator,普通用户是guest.Linux的管理用户是root,普通用户默认有很多个,远 ...