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 Listdn_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; } }}