博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
资源更新
阅读量:5102 次
发布时间:2019-06-13

本文共 3916 字,大约阅读时间需要 13 分钟。

using System;using System.Collections.Generic;using System.Collections;using System.IO;using System.Net;using System.Threading;using UnityEngine;public class Resource_Update : MonoBehaviour{    private Thread m_thread;    private List
dn_list_file = new List
(); private List
dn_list_url = new List
(); private List
dn_list_path = new List
(); private List
dn_list_size = new List
(); private int download_index; private bool download_state; private long download_value; private long download_progress; private string download_url; private string download_path; private void Start() { StartCoroutine(DownloadFile()); } #region Ready ///
/// 从服务器下载所有资源 /// ///
IEnumerator DownloadFile() { WWW www = new WWW(Application.streamingAssetsPath + "/File.txt"); yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.LogError(www.error); } else if (www.isDone) { if (!string.IsNullOrEmpty(www.text)) { string[] files = www.text.Split('\n'); for (int i = 0; i < files.Length; i++) { dn_list_file.Add(files[i].TrimEnd('\r')); } Compare(); } } } ///
/// 服务器资源与本地资源进行比较 /// private void Compare() { dn_list_url.Clear(); dn_list_path.Clear(); for (int i = 0; i < dn_list_file.Count; i++) { if (!string.IsNullOrEmpty(dn_list_file[i])) { string[] line = dn_list_file[i].Split('|'); string[] path = line[0].Split('/'); string local_file = Application.persistentDataPath + "/Res/" + path[path.Length - 1]; if (File.Exists(local_file)) { if (Utils_MD5.MD5_File(local_file) != line[1]) { dn_list_url.Add(line[0]); dn_list_path.Add(local_file); } } else { string dir = Path.GetDirectoryName(local_file); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); dn_list_url.Add(line[0]); dn_list_path.Add(local_file); } } } if (dn_list_url.Count > 0) { download_value = GetFileSize(); StartUp(); } else { Debug.Log("资源已是最新!"); } } ///
/// 获取更新资源大小 /// ///
资源大小
private long GetFileSize() { long size_all = 0; for (int i = 0; i < dn_list_url.Count; i++) { long szie = GetFileSize(dn_list_url[i]); dn_list_size.Add(szie); size_all += szie; } return size_all; } private long GetFileSize(string url) { HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "HEAD"; HttpWebResponse response = request.GetResponse() as HttpWebResponse; return response.ContentLength; } #endregion #region Download private void StartUp() { download_index = 0; Next(); m_thread = new Thread(DownLoad); m_thread.Start(); } private void DownLoad() { while (true) { if (download_state) { try { using (WebClient cilent = new WebClient()) { cilent.DownloadProgressChanged += Progress; cilent.DownloadFileAsync(new Uri(download_url), download_path); } } catch (Exception e) { Debug.LogError(e.Message); } download_state = false; } Thread.Sleep(1); } } private void Progress(object sender, DownloadProgressChangedEventArgs e) { EventMessageArgs arg = new EventMessageArgs(); arg.AddOrReplaceMessage("progress", download_progress + e.BytesReceived); arg.AddOrReplaceMessage("endvalue", download_value); EventManager.PostEvent(EventKey.Download_Progress, arg); if (e.ProgressPercentage == 100) { if (dn_list_url.Count > download_index) { Next(); } else { Finish(); } } } private void Next() { download_url = dn_list_url[download_index]; download_path = dn_list_path[download_index]; if (download_index > 0) download_progress += dn_list_size[download_index - 1]; else download_progress = 0; download_index++; download_state = true; } private void Finish() { if (m_thread != null) { m_thread.Abort(); m_thread = null; } } #endregion private void OnApplicationQuit() { if (m_thread != null) { m_thread.Abort(); m_thread = null; } } private void OnDestroy() { if (m_thread != null) { m_thread.Abort(); m_thread = null; } }}
View Code

 

转载于:https://www.cnblogs.com/Joke-crazy/p/10478544.html

你可能感兴趣的文章
mysql 查看当前连接及修改连接数
查看>>
springboot No Identifier specified for entity的解决办法
查看>>
【Luogu1303】【模板】A*B Problem
查看>>
慵懒中长大的人,只会挨生活留下的耳光
查看>>
HTML——校友会(bootstrap)
查看>>
【分布计算环境学习笔记】2 分布式系统中的面向对象技术
查看>>
Enable SSH Server
查看>>
宝塔部署项目
查看>>
如何终止线程的运行(C/C++)
查看>>
"远程桌面连接--“发生身份验证错误。要求的函数不受支持
查看>>
【BZOJ1565】 植物大战僵尸
查看>>
视频:"我是设计师"高清完整版Plus拍摄花絮
查看>>
sicp solutions
查看>>
mysql数据库常用命令
查看>>
VALSE2019总结(4)-主题报告
查看>>
浅谈 unix, linux, ios, android 区别和联系
查看>>
PhotoZoom放大图片,真的能无损吗?
查看>>
转载分享移动网站最佳实践
查看>>
spark--环境搭建--4.ZooKeeper345集群搭建
查看>>
【Leetcode_easy】1103. Distribute Candies to People
查看>>