123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- using System;
- using System.IO;
- using System.Net;
- using System.Threading;
- using UnityEngine;
- public class EditorDownloadObject {
- private string m_DownloadURL;
- private string m_LocalFilePath;
- private Action m_CallBack;
- //下载进度
- public float progress { get; private set; }
- //涉及子线程要注意,Unity关闭的时候子线程不会关闭,所以要有一个标识
- private bool isStop;
- //表示下载是否完成
- public bool isDone { get; private set; }
- public long currentFileLength { get; private set; }
- public long totalFileLength { get; private set; }
- const int ReadWriteTimeOut = 2 * 1000;//超时等待时间
- const int TimeOutWait = 120 * 1000;//超时等待时间
- private Thread thread;
- FileStream fileStream;
- Stream stream;
- /// <summary>
- /// 下载方法(断点续传)
- /// </summary>
- /// <param name="url">URL下载地址</param>
- /// <param name="savePath">Save path保存路径</param>
- /// <param name="m_path">模型bundle保存路径</param>
- /// <param name="callBack">Call back回调函数</param>
- public void DownLoad(string url, string savePath, Action callBack) {
- isStop = false;
- m_DownloadURL = url;
- m_LocalFilePath = savePath;
- m_CallBack = callBack;
- //开启子线程
- thread = new Thread(DownloadInternal);
- thread.IsBackground = true;
- thread.Start();
- }
- private void DownloadInternal() {
- fileStream = new FileStream(m_LocalFilePath, FileMode.OpenOrCreate, FileAccess.Write);
- currentFileLength = fileStream.Length;
- totalFileLength = GetTotalFileLength(m_DownloadURL);
- if (currentFileLength < totalFileLength) {
- //断点续传核心,设置本地文件流的起始位置
- fileStream.Seek(currentFileLength, SeekOrigin.Begin);
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = HttpWebRequest.Create(m_DownloadURL) as HttpWebRequest;
- request.UseDefaultCredentials = false;
- request.Proxy = null;
- request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
- request.ReadWriteTimeout = ReadWriteTimeOut;
- request.Timeout = TimeOutWait;
- request.KeepAlive = false;
- //断点续传核心,设置远程访问文件流的起始位置
- request.AddRange((int)currentFileLength);
- WebResponse response = request.GetResponse();
- stream = response.GetResponseStream();
- byte[] buffer = new byte[1024];
- //使用流读取内容到buffer中
- int length = stream.Read(buffer, 0, buffer.Length);
- while (length > 0) {
- //如果Unity客户端关闭,停止下载
- if (isStop) {
- break;
- }
- fileStream.Write(buffer, 0, length);
- currentFileLength += length;
- progress = (float)currentFileLength / (float)totalFileLength;
- length = stream.Read(buffer, 0, buffer.Length);
- }
- stream.Close();
- stream.Dispose();
- } else {
- progress = 1;
- }
- fileStream.Close();
- fileStream.Dispose();
- //如果下载完毕,执行回调
- if (progress == 1) {
- isDone = true;
- if (m_CallBack != null) m_CallBack();
- Debug.Log("download finished");
- }
- thread.Abort();
- }
- /// <summary>
- /// 获取下载文件的大小
- /// </summary>
- /// <returns>The length.</returns>
- /// <param name="url">URL.</param>
- long GetTotalFileLength(string url) {
- HttpWebRequest requet = HttpWebRequest.Create(url) as HttpWebRequest;
- requet.Method = "HEAD";
- HttpWebResponse response = requet.GetResponse() as HttpWebResponse;
- return response.ContentLength;
- }
- public void Dispose() {
- isStop = true;
- if (fileStream != null) {
- fileStream.Close();
- fileStream.Dispose();
- }
- if (stream != null) {
- stream.Close();
- stream.Dispose();
- }
- if (thread != null) {
- thread.Abort();
- }
- Debug.LogFormat("EditorDownloadObject.Dispose: {0}", m_DownloadURL);
- }
- }
|