UnityWebDataRequester.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.Networking;
  5. namespace YooAsset
  6. {
  7. /// <summary>
  8. /// 下载器
  9. /// 说明:UnityWebRequest(UWR) supports reading streaming assets since 2017.1
  10. /// </summary>
  11. internal class UnityWebDataRequester
  12. {
  13. private UnityWebRequest _webRequest;
  14. private UnityWebRequestAsyncOperation _operationHandle;
  15. /// <summary>
  16. /// 请求URL地址
  17. /// </summary>
  18. public string URL { private set; get; }
  19. /// <summary>
  20. /// 发送GET请求
  21. /// </summary>
  22. /// <param name="timeout">超时:从请求开始计时</param>
  23. public void SendRequest(string url, int timeout = 0)
  24. {
  25. if (_webRequest == null)
  26. {
  27. URL = url;
  28. _webRequest = DownloadSystem.NewRequest(URL);
  29. DownloadHandlerBuffer handler = new DownloadHandlerBuffer();
  30. _webRequest.downloadHandler = handler;
  31. _webRequest.disposeDownloadHandlerOnDispose = true;
  32. _webRequest.timeout = timeout;
  33. _operationHandle = _webRequest.SendWebRequest();
  34. }
  35. }
  36. /// <summary>
  37. /// 获取下载的字节数据
  38. /// </summary>
  39. public byte[] GetData()
  40. {
  41. if (_webRequest != null && IsDone())
  42. return _webRequest.downloadHandler.data;
  43. else
  44. return null;
  45. }
  46. /// <summary>
  47. /// 获取下载的文本数据
  48. /// </summary>
  49. public string GetText()
  50. {
  51. if (_webRequest != null && IsDone())
  52. return _webRequest.downloadHandler.text;
  53. else
  54. return null;
  55. }
  56. /// <summary>
  57. /// 释放下载器
  58. /// </summary>
  59. public void Dispose()
  60. {
  61. if (_webRequest != null)
  62. {
  63. _webRequest.Dispose();
  64. _webRequest = null;
  65. _operationHandle = null;
  66. }
  67. }
  68. /// <summary>
  69. /// 是否完毕(无论成功失败)
  70. /// </summary>
  71. public bool IsDone()
  72. {
  73. if (_operationHandle == null)
  74. return false;
  75. return _operationHandle.isDone;
  76. }
  77. /// <summary>
  78. /// 下载进度
  79. /// </summary>
  80. public float Progress()
  81. {
  82. if (_operationHandle == null)
  83. return 0;
  84. return _operationHandle.progress;
  85. }
  86. /// <summary>
  87. /// 下载是否发生错误
  88. /// </summary>
  89. public bool HasError()
  90. {
  91. #if UNITY_2020_3_OR_NEWER
  92. return _webRequest.result != UnityWebRequest.Result.Success;
  93. #else
  94. if (_webRequest.isNetworkError || _webRequest.isHttpError)
  95. return true;
  96. else
  97. return false;
  98. #endif
  99. }
  100. /// <summary>
  101. /// 获取错误信息
  102. /// </summary>
  103. public string GetError()
  104. {
  105. if (_webRequest != null)
  106. {
  107. return $"URL : {URL} Error : {_webRequest.error}";
  108. }
  109. return string.Empty;
  110. }
  111. }
  112. }