DownloadTask.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. public enum DownloadTaskState
  8. {
  9. None,
  10. Downloading,
  11. DownloadSuccess,
  12. Error,
  13. }
  14. public class DownloadTask : MonoBehaviour
  15. {
  16. private UnityWebRequest webRqst;
  17. public UnityWebRequest WebRqst { get => webRqst; }
  18. public Action<DownloadTask> Callback { get; set; }
  19. [SerializeField]
  20. public DownloadDataEntity CurDownloadEntity { get; set; }
  21. [SerializeField]
  22. public float CurLoadProgress { get; private set; }
  23. public ulong downloadSize;
  24. private float timeOut;
  25. private DownloadTaskState state;
  26. public DownloadTaskState State {
  27. get => state;
  28. private set
  29. {
  30. state = value;
  31. if (CurDownloadEntity != null)
  32. {
  33. CurDownloadEntity.State = state;
  34. }
  35. }
  36. }
  37. public ulong DownLoadSize { get => webRqst.downloadedBytes; }
  38. public DownloadTask()
  39. {
  40. //webRqst = new UnityWebRequest();
  41. }
  42. public bool SetUrl(string url = "")
  43. {
  44. bool ret = true;
  45. if (webRqst!= null)
  46. {
  47. webRqst.Dispose();
  48. webRqst = null;
  49. }
  50. if (string.IsNullOrEmpty(url) && CurDownloadEntity != null)
  51. {
  52. webRqst = UnityWebRequest.Get(DownloadMgr.Instance.DownloadUrl + CurDownloadEntity.FullName);
  53. }
  54. else if (!string.IsNullOrEmpty(url))
  55. {
  56. webRqst = UnityWebRequest.Get(url);
  57. }
  58. else
  59. {
  60. ret = false;
  61. }
  62. webRqst.timeout = 5;
  63. return ret;
  64. }
  65. public void StartTask()
  66. {
  67. StartCoroutine(Task());
  68. }
  69. public IEnumerator Task()
  70. {
  71. if (State == DownloadTaskState.Downloading)
  72. {
  73. yield break;
  74. }
  75. State = DownloadTaskState.Downloading;
  76. string savePath = DownloadMgr.Instance.LocalFilePath + CurDownloadEntity.FullName;
  77. UnityWebRequestAsyncOperation asyncOperation = webRqst.SendWebRequest();
  78. yield return null;
  79. timeOut = Time.time;
  80. CurLoadProgress = 0;
  81. downloadSize = 0;
  82. while (!webRqst.isDone)
  83. {
  84. if (CurLoadProgress < webRqst.downloadProgress)
  85. {
  86. timeOut = Time.time;
  87. CurLoadProgress = webRqst.downloadProgress;
  88. downloadSize = webRqst.downloadedBytes;
  89. CurDownloadEntity.UpDateCallback?.Invoke(this);
  90. }
  91. if (Time.time - timeOut > DownloadMgr.TimeOut)
  92. {
  93. Debug.LogWarning("下载超时!!!");
  94. //fs.Close();
  95. State = DownloadTaskState.Error;
  96. Callback?.Invoke(this);
  97. yield break;
  98. }
  99. yield return null;
  100. }
  101. CurLoadProgress = webRqst.downloadProgress;
  102. downloadSize = webRqst.downloadedBytes;
  103. CurDownloadEntity.UpDateCallback?.Invoke(this);
  104. if (webRqst != null && webRqst.error == null)
  105. {
  106. Debug.Log($"[{CurDownloadEntity.FullName}] 下载完成");
  107. State = DownloadTaskState.DownloadSuccess;
  108. FileHelper.WirteToFile( savePath,webRqst.downloadHandler.data);
  109. Callback?.Invoke(this);
  110. }
  111. else
  112. {
  113. State = DownloadTaskState.Error;
  114. Debug.Log($"下载失败;Error = {webRqst.error} === {CurDownloadEntity.FullName} ");
  115. Callback?.Invoke(this);
  116. }
  117. webRqst.Dispose();
  118. }
  119. }