123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using LitJson;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using UnityEngine.Networking;
- public class LocalLoadManager : MonoSingleton<LocalLoadManager>
- {
-
-
-
- private Dictionary<string, byte[]> dicLoaclData = new Dictionary<string, byte[]>();
- public void test()
- {
- Debug.Log("Hjj test");
- }
- public void LocalLoadMaterial(DownLoadMaterial downloadMaterial)
- {
-
- if (dicLoaclData.ContainsKey(downloadMaterial.downLoadPath))
- {
-
- MsgHandler.SendMsg(downloadMaterial.downLoadPath, new Msg(downloadMaterial.downLoadPath, dicLoaclData[downloadMaterial.downLoadPath]));
- }
- else
- {
-
-
- StartCoroutine(DownloadFileIO(downloadMaterial));
- }
- }
- private System.Collections.IEnumerator DownloadFile(DownLoadMaterial downloadMaterial)
- {
- bool isDownLoad;
- Debug.Log("Hjj DownloadFile :" + downloadMaterial.localLoadPath + " " + downloadMaterial.type);
- 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");
- Debug.Log("Hjj DownloadFile start:" + downloadMaterial.localLoadPath + " ");
- yield return webRequest.SendWebRequest();
- Debug.Log("Hjj DownloadFile end:" + downloadMaterial.localLoadPath + " ");
- while (!webRequest.isDone)
- {
-
-
-
-
- yield return new WaitForFixedUpdate();
- }
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError("Hjj DownloadFile end:" + downloadMaterial.localLoadPath + "Download Failed: " + webRequest.error);
-
-
- isDownLoad = false;
- MsgHandler.SendMsg(downloadMaterial.downLoadPath, new Msg(downloadMaterial.downLoadPath, null));
- }
- else
- {
- Debug.Log("Hjj DownloadFile isDone:"+webRequest.isDone + " " + webRequest.downloadHandler.data.Length);
-
- dicLoaclData.Add(downloadMaterial.downLoadPath, webRequest.downloadHandler.data);
- isDownLoad = true;
- MsgHandler.SendMsg(downloadMaterial.downLoadPath, new Msg(downloadMaterial.downLoadPath, webRequest.downloadHandler.data));
- }
-
- }
- }
-
-
-
- private void ReadLocaData(DownLoadMaterial downloadMaterial)
- {
-
- }
- IEnumerator DownloadFileIO(DownLoadMaterial downloadMaterial)
- {
- if (!File.Exists(downloadMaterial.localLoadPath))
- {
- Debug.Log("DGJ ====》 文件不存在 " + downloadMaterial.localLoadPath);
- MsgHandler.SendMsg(downloadMaterial.downLoadPath, new Msg(downloadMaterial.downLoadPath, null));
- }
- else
- {
- using (FileStream fs = new FileStream(downloadMaterial.localLoadPath, FileMode.Open))
- {
- long fileSize = new FileInfo(downloadMaterial.localLoadPath).Length;
- byte[] buffer = new byte[fileSize];
- int bytesRead;
- while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
- {
-
-
- }
- yield return bytesRead == 0;
- dicLoaclData.Add(downloadMaterial.downLoadPath, buffer);
-
- MsgHandler.SendMsg(downloadMaterial.downLoadPath, new Msg(downloadMaterial.downLoadPath, buffer));
- }
- }
-
- }
- }
|