UnityWebFileRequester.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.Networking;
  5. using UnityEngine;
  6. namespace YooAsset
  7. {
  8. /// <summary>
  9. /// 下载器
  10. /// 说明:UnityWebRequest(UWR) supports reading streaming assets since 2017.1
  11. /// </summary>
  12. internal class UnityWebFileRequester
  13. {
  14. private UnityWebRequest _webRequest;
  15. private UnityWebRequestAsyncOperation _operationHandle;
  16. // 超时相关
  17. private float _timeout;
  18. private bool _isAbort = false;
  19. private ulong _latestDownloadBytes;
  20. private float _latestDownloadRealtime;
  21. /// <summary>
  22. /// 请求URL地址
  23. /// </summary>
  24. public string URL { private set; get; }
  25. /// <summary>
  26. /// 发送GET请求
  27. /// </summary>
  28. public void SendRequest(string url, string savePath, float timeout = 60)
  29. {
  30. if (_webRequest == null)
  31. {
  32. URL = url;
  33. _timeout = timeout;
  34. _latestDownloadBytes = 0;
  35. _latestDownloadRealtime = Time.realtimeSinceStartup;
  36. _webRequest = DownloadSystem.NewRequest(URL);
  37. DownloadHandlerFile handler = new DownloadHandlerFile(savePath);
  38. handler.removeFileOnAbort = true;
  39. _webRequest.downloadHandler = handler;
  40. _webRequest.disposeDownloadHandlerOnDispose = true;
  41. _operationHandle = _webRequest.SendWebRequest();
  42. }
  43. }
  44. /// <summary>
  45. /// 释放下载器
  46. /// </summary>
  47. public void Dispose()
  48. {
  49. if (_webRequest != null)
  50. {
  51. _webRequest.Dispose();
  52. _webRequest = null;
  53. _operationHandle = null;
  54. }
  55. }
  56. /// <summary>
  57. /// 是否完毕(无论成功失败)
  58. /// </summary>
  59. public bool IsDone()
  60. {
  61. if (_operationHandle == null)
  62. return false;
  63. return _operationHandle.isDone;
  64. }
  65. /// <summary>
  66. /// 下载进度
  67. /// </summary>
  68. public float Progress()
  69. {
  70. if (_operationHandle == null)
  71. return 0;
  72. return _operationHandle.progress;
  73. }
  74. /// <summary>
  75. /// 下载是否发生错误
  76. /// </summary>
  77. public bool HasError()
  78. {
  79. #if UNITY_2020_3_OR_NEWER
  80. return _webRequest.result != UnityWebRequest.Result.Success;
  81. #else
  82. if (_webRequest.isNetworkError || _webRequest.isHttpError)
  83. return true;
  84. else
  85. return false;
  86. #endif
  87. }
  88. /// <summary>
  89. /// 获取错误信息
  90. /// </summary>
  91. public string GetError()
  92. {
  93. if (_webRequest != null)
  94. {
  95. return $"URL : {URL} Error : {_webRequest.error}";
  96. }
  97. return string.Empty;
  98. }
  99. /// <summary>
  100. /// 检测超时
  101. /// </summary>
  102. public void CheckTimeout()
  103. {
  104. // 注意:在连续时间段内无新增下载数据及判定为超时
  105. if (_isAbort == false)
  106. {
  107. if (_latestDownloadBytes != _webRequest.downloadedBytes)
  108. {
  109. _latestDownloadBytes = _webRequest.downloadedBytes;
  110. _latestDownloadRealtime = Time.realtimeSinceStartup;
  111. }
  112. float offset = Time.realtimeSinceStartup - _latestDownloadRealtime;
  113. if (offset > _timeout)
  114. {
  115. _webRequest.Abort();
  116. _isAbort = true;
  117. }
  118. }
  119. }
  120. }
  121. }