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