123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- using UnityEngine.Networking;
- public class DownLoadItem : MonoBehaviour
- {
- public delegate void DownLoadCallBack(bool b);
- public delegate void DownLoadPressOn(float f);
-
- public DownLoadCallBack callback;
-
- public DownLoadPressOn onProgress;
-
- public string id;
-
-
-
- public string downLoadPath;
-
-
-
- public bool isDownLoad;
-
-
-
- public byte[] downLoadData;
-
-
-
- public float progress;
-
-
-
- public string downLoadState;
-
- public bool isAction;
- private UnityWebRequest webRequest;
- public void Init(string downLoadPath,string id)
- {
- this.downLoadPath = downLoadPath;
- this.id = id;
- LoadManager.Instance.downloadQueueList.Enqueue(this);
- }
- public System.Collections.IEnumerator DownloadFile()
- {
- string url = HttpAction.baseurl + HttpAction.file_download;
- if (UserInfo.Instance.is20)
- {
- url = HttpAction.baseurl + HttpAction.file_download;
- }
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(downLoadPath);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
- webRequest.SendWebRequest();
- while (!webRequest.isDone)
- {
-
- progress = webRequest.downloadProgress;
- onProgress?.Invoke(progress);
-
- yield return null;
- }
- if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError)
- {
- Debug.LogError("Download Failed: " + webRequest.error);
-
- downLoadState = webRequest.error;
- isDownLoad = false;
- }
- else
- {
- downLoadState = "下载成功";
- downLoadData = webRequest.downloadHandler.data;
- isDownLoad = true;
- }
- callback?.Invoke(isDownLoad);
- }
- }
- private void OnDestroy()
- {
-
- if (webRequest != null && !webRequest.isDone)
- {
- webRequest.Abort();
- }
- }
- }
|