UnityWebHttpDownload.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. public class UnityWebHttpDownload : HttpDownloadBasic
  6. {
  7. private UnityWebRequest _unityWebRequest;
  8. private UnityWebRequestAsyncOperation _unityWebRequestAsyncOperation;
  9. protected float currTime;
  10. protected override void ProStartDown()
  11. {
  12. maxCount--;
  13. isFinish = false;
  14. currTime = 0;
  15. if (_unityWebRequest != null)
  16. {
  17. _unityWebRequest.Dispose();
  18. _unityWebRequest = null;
  19. }
  20. _unityWebRequest = UnityWebRequest.Get(downloadFileData.remoteUrl);
  21. if (!string.IsNullOrEmpty(downloadFileData.localPath))
  22. {
  23. _unityWebRequest.downloadHandler = new DownloadHandlerFile(downloadFileData.localPath);
  24. }
  25. _unityWebRequest.timeout = timeOut;
  26. // _unityWebRequest.
  27. _unityWebRequestAsyncOperation = _unityWebRequest.SendWebRequest();
  28. DownloadHander.UnityWebRequestAsyncOperation = _unityWebRequestAsyncOperation;
  29. }
  30. protected override bool ProFinish()
  31. {
  32. return _unityWebRequestAsyncOperation.isDone;
  33. }
  34. public override bool AllFinish()
  35. {
  36. return isFinish;
  37. }
  38. public override UnityWebRequest.Result Result()
  39. {
  40. return _unityWebRequest.result;
  41. }
  42. public override void Update()
  43. {
  44. currTime += Time.deltaTime;
  45. if (!isFinish && ProFinish())
  46. {
  47. isFinish = true;
  48. if (_unityWebRequest.result != UnityWebRequest.Result.Success)
  49. {
  50. Debug.Log("下载失败" + _unityWebRequest.error + " " + downloadFileData.remoteUrl);
  51. if (maxCount > 0)
  52. {
  53. ProStartDown();
  54. }
  55. else
  56. {
  57. DownloadHander.OnFinishCallBack?.Invoke(DownloadHander);
  58. }
  59. }
  60. else
  61. {
  62. DownloadHander.OnFinishCallBack?.Invoke(DownloadHander);
  63. }
  64. }
  65. else
  66. {
  67. if (currTime > timeOut)
  68. {
  69. Debug.Log("超时"+currTime+"__"+timeOut);
  70. ProStartDown();
  71. }
  72. }
  73. }
  74. }