| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using UnityEngine.Networking;
- public enum DownloadTaskState
- {
- None,
- Downloading,
- DownloadSuccess,
- Error,
- }
- public class DownloadTask : MonoBehaviour
- {
- private UnityWebRequest webRqst;
- public UnityWebRequest WebRqst { get => webRqst; }
- public Action<DownloadTask> Callback { get; set; }
- [SerializeField]
- public DownloadDataEntity CurDownloadEntity { get; set; }
- [SerializeField]
- public float CurLoadProgress { get; private set; }
- public ulong downloadSize;
- private float timeOut;
- private DownloadTaskState state;
- public DownloadTaskState State {
- get => state;
- private set
- {
- state = value;
- if (CurDownloadEntity != null)
- {
- CurDownloadEntity.State = state;
- }
- }
- }
- public ulong DownLoadSize { get => webRqst.downloadedBytes; }
-
- public DownloadTask()
- {
- //webRqst = new UnityWebRequest();
- }
- public bool SetUrl(string url = "")
- {
- bool ret = true;
- if (webRqst!= null)
- {
- webRqst.Dispose();
- webRqst = null;
- }
- if (string.IsNullOrEmpty(url) && CurDownloadEntity != null)
- {
- webRqst = UnityWebRequest.Get(DownloadMgr.Instance.DownloadUrl + CurDownloadEntity.FullName);
- }
- else if (!string.IsNullOrEmpty(url))
- {
- webRqst = UnityWebRequest.Get(url);
- }
- else
- {
- ret = false;
- }
- webRqst.timeout = 20;
- return ret;
- }
- public void StartTask()
- {
- StartCoroutine(Task());
- }
- public IEnumerator Task()
- {
- if (State == DownloadTaskState.Downloading)
- {
- yield break;
- }
- State = DownloadTaskState.Downloading;
-
-
- string savePath = DownloadMgr.Instance.LocalFilePath + CurDownloadEntity.FullName;
- UnityWebRequestAsyncOperation asyncOperation = webRqst.SendWebRequest();
- yield return null;
- timeOut = Time.time;
- CurLoadProgress = 0;
- downloadSize = 0;
- while (!webRqst.isDone)
- {
- if (CurLoadProgress < webRqst.downloadProgress)
- {
- timeOut = Time.time;
- CurLoadProgress = webRqst.downloadProgress;
-
- downloadSize = webRqst.downloadedBytes;
- CurDownloadEntity.UpDateCallback?.Invoke(this);
- }
- if (Time.time - timeOut > DownloadMgr.TimeOut)
- {
- Debug.LogWarning("下载超时!!!");
- //fs.Close();
- State = DownloadTaskState.Error;
- Callback?.Invoke(this);
- yield break;
- }
- yield return null;
- }
- CurLoadProgress = webRqst.downloadProgress;
- downloadSize = webRqst.downloadedBytes;
- CurDownloadEntity.UpDateCallback?.Invoke(this);
-
- if (webRqst != null && webRqst.result == UnityWebRequest.Result.Success)
- {
- Debug.Log($"[{CurDownloadEntity.FullName}] 下载完成");
- State = DownloadTaskState.DownloadSuccess;
- FileHelper.WirteToFile( savePath,webRqst.downloadHandler.data);
- Callback?.Invoke(this);
- }
- else
- {
- State = DownloadTaskState.Error;
- Debug.Log($"下载失败;Error = {webRqst.error} === {CurDownloadEntity.FullName} ");
- Callback?.Invoke(this);
- }
- webRqst.Dispose();
-
- }
- }
|