EditorDownloadObject.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Threading;
  5. using UnityEngine;
  6. public class EditorDownloadObject {
  7. private string m_DownloadURL;
  8. private string m_LocalFilePath;
  9. private Action m_CallBack;
  10. //下载进度
  11. public float progress { get; private set; }
  12. //涉及子线程要注意,Unity关闭的时候子线程不会关闭,所以要有一个标识
  13. private bool isStop;
  14. //表示下载是否完成
  15. public bool isDone { get; private set; }
  16. public long currentFileLength { get; private set; }
  17. public long totalFileLength { get; private set; }
  18. const int ReadWriteTimeOut = 2 * 1000;//超时等待时间
  19. const int TimeOutWait = 120 * 1000;//超时等待时间
  20. private Thread thread;
  21. FileStream fileStream;
  22. Stream stream;
  23. /// <summary>
  24. /// 下载方法(断点续传)
  25. /// </summary>
  26. /// <param name="url">URL下载地址</param>
  27. /// <param name="savePath">Save path保存路径</param>
  28. /// <param name="m_path">模型bundle保存路径</param>
  29. /// <param name="callBack">Call back回调函数</param>
  30. public void DownLoad(string url, string savePath, Action callBack) {
  31. isStop = false;
  32. m_DownloadURL = url;
  33. m_LocalFilePath = savePath;
  34. m_CallBack = callBack;
  35. //开启子线程
  36. thread = new Thread(DownloadInternal);
  37. thread.IsBackground = true;
  38. thread.Start();
  39. }
  40. private void DownloadInternal() {
  41. fileStream = new FileStream(m_LocalFilePath, FileMode.OpenOrCreate, FileAccess.Write);
  42. currentFileLength = fileStream.Length;
  43. totalFileLength = GetTotalFileLength(m_DownloadURL);
  44. if (currentFileLength < totalFileLength) {
  45. //断点续传核心,设置本地文件流的起始位置
  46. fileStream.Seek(currentFileLength, SeekOrigin.Begin);
  47. ServicePointManager.DefaultConnectionLimit = 200;
  48. HttpWebRequest request = HttpWebRequest.Create(m_DownloadURL) as HttpWebRequest;
  49. request.UseDefaultCredentials = false;
  50. request.Proxy = null;
  51. request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
  52. request.ReadWriteTimeout = ReadWriteTimeOut;
  53. request.Timeout = TimeOutWait;
  54. request.KeepAlive = false;
  55. //断点续传核心,设置远程访问文件流的起始位置
  56. request.AddRange((int)currentFileLength);
  57. WebResponse response = request.GetResponse();
  58. stream = response.GetResponseStream();
  59. byte[] buffer = new byte[1024];
  60. //使用流读取内容到buffer中
  61. int length = stream.Read(buffer, 0, buffer.Length);
  62. while (length > 0) {
  63. //如果Unity客户端关闭,停止下载
  64. if (isStop) {
  65. break;
  66. }
  67. fileStream.Write(buffer, 0, length);
  68. currentFileLength += length;
  69. progress = (float)currentFileLength / (float)totalFileLength;
  70. length = stream.Read(buffer, 0, buffer.Length);
  71. }
  72. stream.Close();
  73. stream.Dispose();
  74. } else {
  75. progress = 1;
  76. }
  77. fileStream.Close();
  78. fileStream.Dispose();
  79. //如果下载完毕,执行回调
  80. if (progress == 1) {
  81. isDone = true;
  82. if (m_CallBack != null) m_CallBack();
  83. Debug.Log("download finished");
  84. }
  85. thread.Abort();
  86. }
  87. /// <summary>
  88. /// 获取下载文件的大小
  89. /// </summary>
  90. /// <returns>The length.</returns>
  91. /// <param name="url">URL.</param>
  92. long GetTotalFileLength(string url) {
  93. HttpWebRequest requet = HttpWebRequest.Create(url) as HttpWebRequest;
  94. requet.Method = "HEAD";
  95. HttpWebResponse response = requet.GetResponse() as HttpWebResponse;
  96. return response.ContentLength;
  97. }
  98. public void Dispose() {
  99. isStop = true;
  100. if (fileStream != null) {
  101. fileStream.Close();
  102. fileStream.Dispose();
  103. }
  104. if (stream != null) {
  105. stream.Close();
  106. stream.Dispose();
  107. }
  108. if (thread != null) {
  109. thread.Abort();
  110. }
  111. Debug.LogFormat("EditorDownloadObject.Dispose: {0}", m_DownloadURL);
  112. }
  113. }