1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- public class LocalLoadManager : MonoSingleton<LocalLoadManager>
- {
- /// <summary>
- /// 已下载资源
- /// </summary>
- private Dictionary<string, byte[]> dicLoaclData = new Dictionary<string, byte[]>();
- public void LocalLoadMaterial(DownLoadMaterial downloadMaterial)
- {
- if (dicLoaclData.ContainsKey(downloadMaterial.localLoadPath))
- {
- MsgHandler.SendMsg(downloadMaterial.localLoadPath, new Msg(downloadMaterial.localLoadPath, dicLoaclData[downloadMaterial.localLoadPath]));
- }
- else
- StartCoroutine(DownloadFile(downloadMaterial));
- }
- private System.Collections.IEnumerator DownloadFile(DownLoadMaterial downloadMaterial)
- {
- bool isDownLoad;
- using (UnityWebRequest webRequest = new UnityWebRequest(downloadMaterial.localLoadPath, UnityWebRequest.kHttpVerbGET))
- {
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
- webRequest.SetRequestHeader("Content-Type", "application/json");
- yield return webRequest.SendWebRequest();
- while (!webRequest.isDone)
- {
- // 此处可以显示下载进度条等UI操作
- //progress = webRequest.downloadProgress;
- //onProgress?.Invoke(progress);
- // Debug.Log("Download Progress: " + progress);
- yield return new WaitForFixedUpdate();
- }
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError("Download Failed: " + webRequest.error);
- // 下载失败
- //downLoadState = webRequest.error;
- isDownLoad = false;
- MsgHandler.SendMsg(downloadMaterial.localLoadPath, new Msg(downloadMaterial.localLoadPath,null));
- }
- else
- {
- Debug.Log(webRequest.isDone + " " + webRequest.downloadHandler.data.Length);
- // Debug.LogError("Download Failed: " + webRequest.error);
- dicLoaclData.Add(downloadMaterial.localLoadPath, webRequest.downloadHandler.data);
- isDownLoad = true;
- MsgHandler.SendMsg(downloadMaterial.localLoadPath, new Msg(downloadMaterial.localLoadPath, webRequest.downloadHandler.data));
- }
-
- }
- }
- }
|