using UnityEngine; using System.Collections; using System.Collections.Generic; using System.IO; using System; /// /// 下载器 /// public class DownloadHelper : MonoBehaviour { /// /// 单例 /// private static DownloadHelper _instance; /// /// 获得单例 /// /// 单例 public static DownloadHelper GetInstance() { if (null == _instance) { GameObject obj = new GameObject("DownloadHelper"); _instance = obj.AddComponent(); } return _instance; } /// /// Awake方法 /// private void Awake() { _instance = this; } #region ==========> 资源批量下载临时变量 /// /// url列表 /// private List _urls = new List(); /// /// 总数 /// private int _count = 0; /// /// 当前索引 /// private int _index = 0; /// /// 下载的总字节数 /// private int _total = 0; /// /// 当前www /// private WWW _www; /// /// 是否中断 /// private bool _breaking = false; /// /// cdn根路径 /// private string _cdnroot = "http://"; /// /// 当前WWW组件,用于监听单文件下载进度,有可能为空 /// public WWW curWWW { get { return _www; } } /// /// 当前文件处理流程 /// public float progress { get { return _www == null ? 0 : _www.progress; } } /// /// 递归下载 /// /// 进程回调 /// 完成回调 /// 错误回调 private void DownLoadItem(ProgressCallback progress, CompleteCallback complete, ErrorCallback onError) { if (_breaking) { return; } string url = _urls[_index]; DownLoad(_cdnroot, url, (token) => { _index++; progress.Invoke(_index, _count, _total); if (_index >= _count) { complete.Invoke("complete!"); } else { DownLoadItem(progress, complete, onError); } }, onError); } #endregion /// /// 下载资源列表 /// /// cnd根目录 /// url路径 /// 进程回调 /// 完成回调 /// 错误回调 public void DownLoadList(string cdnroot, List urls, ProgressCallback progress, CompleteCallback complete, ErrorCallback onError) { _cdnroot = cdnroot; _breaking = false; _urls.Clear(); _urls = urls; _count = urls.Count; _index = 0; _total = 0; DownLoadItem(progress, complete, onError); } /// /// 中断列表下载(仅适用于列表下载) /// public void Break() { _breaking = true; } /// /// 单文件下载 /// /// cdn根路径 /// url路径 /// 完成回调 /// 错误回调 public void DownLoad(string cdnroot, string url, CompleteCallback callback, ErrorCallback onError) { _cdnroot = cdnroot; StartCoroutine(IDownload(url, callback, onError)); } /// /// 协同 /// /// url /// 回调 /// 错误回调 /// 协同句柄 private IEnumerator IDownload(string url, CompleteCallback callback, ErrorCallback onError) { if (_www != null) { _www.Dispose(); _www = null; } WWW w = new WWW(_cdnroot + url); _www = w; DateTime start = DateTime.Now; /* // 超时检测 while (false == w.isDone) { if (DateTime.Now - start > TimeSpan.FromSeconds(Const.NetDefaultTimeOut / 1000)) { onError("Time out!!"); yield break; } else { yield return null; } } * */ yield return w; if (w.error != null) { LogHelper.LogWarning(w.error); onError(w.error); } else { LogHelper.Log("[DownLoad]::" + url); CreateFile(FilePath(url), w.bytes); _total += w.size; callback("ok!"); } } /// /// 创建文件 /// /// 路径 /// 内容 public static void CreateFile(string path, byte[] content) { string dir = GetDirectoryByPath(path); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } FileStream fs = new FileStream(path, FileMode.Create); fs.Write(content, 0, content.Length); fs.Flush(); fs.Close(); } /// /// 根据文件路径截取目录 /// /// 路径 /// 目录 public static string GetDirectoryByPath(string path) { string[] temps = path.Split('/'); string dir = string.Empty; for (int i = 0; i < temps.Length - 1; i++) { dir += temps[i] + "/"; } return dir; } /// /// 获得文件路径 /// /// url /// 路径 private string FilePath(string url) { return Application.persistentDataPath + "/" + url; } } /// /// 错误回调 /// /// 错误消息 public delegate void ErrorCallback(string msg); /// /// 完成回调 /// /// token public delegate void CompleteCallback(object token); /// /// 进程回调 /// /// 当前索引 /// 总数 /// 总字节数 public delegate void ProgressCallback(int index, int count, int total);