首页 > 代码库 > Unity3d热更新之下载
Unity3d热更新之下载
热更新框架:uluaFramework_UIGUI-master,在Unity热更新时,先释放资源(StreamingAssets --> DataPath),再从服务端下载资源(或者其它下载网络文件的时候)
(1)下载方案
启动一个线程,每隔几毫秒询问队列,队列中下载Action数大于0,则开启异步下载
(2)异步下载异常处理
我用windows自带的iis搭建的web服务,在下载资源时始终下载不下来,但又没有异常提示。luaFramework使用WebClient.DownloadFileAsync时,若下载发生异常,并没有做异常处理
解决办法:
WebClient异步下载时,tryCatch捕捉不到异常,需要在WebClient.DownloadFileComplete回调中捕捉
using UnityEngine; using System.Collections; using System.Threading; using System.Collections.Generic; using System; using System.Net; using System.Diagnostics; using Debug = UnityEngine.Debug; using System.ComponentModel; /// <summary> /// 下载事务 /// </summary> public class DownloadEvent { /// <summary> /// 下载事务名称(这里是下载的文件名) /// </summary> public string evName; /// <summary> /// 下载的远程、本地Url /// </summary> public List<object> evParams = new List<object>(); public DownloadEvent(string name, List<object> list) { this.evName = name; this.evParams = list; } } public class ThreadManager : MonoBehaviour { public Thread thread; public Queue<DownloadEvent> events = new Queue<DownloadEvent>(); /// <summary> /// 计时器 /// </summary> Stopwatch sw = new Stopwatch(); static readonly object lockObject = new object(); void Start() { thread = new Thread(OnUpdate); thread.Start(); List<object> list1 = new List<object>(); list1.Add("http://localhost/StreamingAssets/StreamingAssets"); list1.Add("d:/StreamingAssets/StreamingAssets"); DownloadEvent ev1 = new DownloadEvent("StreamingAssets", list1); List<object> list2 = new List<object>(); list2.Add("http://localhost/StreamingAssets/shared_asset.unity3d"); list2.Add("d:/StreamingAssets/shared_asset.unity3d"); DownloadEvent ev2 = new DownloadEvent("shared_asset.unity3d", list2); events.Enqueue(ev1); events.Enqueue(ev2); } void OnUpdate() { while (true) { lock (lockObject) { if (events.Count > 0) { Debug.Log(events.Count); DownloadEvent ev = events.Dequeue(); DownloadFile(ev); Debug.Log("开始下载文件:" + ev.evName); } } Thread.Sleep(1); } } /// <summary> /// 下载文件 /// </summary> /// <param name="ev">下载事务</param> private void DownloadFile(DownloadEvent ev) { //url string url = ev.evParams[0].ToString(); string localPath = ev.evParams[1].ToString(); using (WebClient client = new WebClient()) { //sw.Start(); //计时器,可以用于计算下载速度 client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProcesChange); client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadComplete); client.DownloadFileAsync(new Uri(url), ev.evName); } } /// <summary> /// 异常处理回调 /// </summary> private void DownloadComplete(object sender, AsyncCompletedEventArgs e) { if (e.Error!=null) { Debug.LogError(e.Error.Message); } } /// <summary> /// 下载回调 /// </summary> private void DownloadProcesChange(object sender, DownloadProgressChangedEventArgs e) { string received = (e.BytesReceived / 1024d) + ""; Debug.Log("sender:" + sender); Debug.Log("接收文件大小:" + received + "K"); if (e.ProgressPercentage == 100 && e.BytesReceived == e.TotalBytesToReceive) { Debug.Log("下载完成"); } } }
Unity3d热更新之下载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。