LocalLoadManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using LitJson;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. public class LocalLoadManager : MonoSingleton<LocalLoadManager>
  8. {
  9. /// <summary>
  10. /// 已下载资源
  11. /// </summary>
  12. private Dictionary<string, byte[]> dicLoaclData = new Dictionary<string, byte[]>();
  13. public void test()
  14. {
  15. Debug.Log("Hjj test");
  16. }
  17. public void LocalLoadMaterial(DownLoadMaterial downloadMaterial)
  18. {
  19. //Debug.Log("Hjj LocalLoadMaterial :" + downloadMaterial.localLoadPath + " " + downloadMaterial.type);
  20. if (dicLoaclData.ContainsKey(downloadMaterial.downLoadPath))
  21. {
  22. // Debug.Log("Hjj LocalLoadMaterial 1 :" + downloadMaterial.localLoadPath + " ");
  23. MsgHandler.SendMsg(downloadMaterial.downLoadPath, new Msg(downloadMaterial.downLoadPath, dicLoaclData[downloadMaterial.downLoadPath]));
  24. }
  25. else
  26. {
  27. // Debug.Log("Hjj LocalLoadMaterial 2 :" + downloadMaterial.localLoadPath + " ");
  28. // StartCoroutine(DownloadFile(downloadMaterial));
  29. StartCoroutine(DownloadFileIO(downloadMaterial));
  30. }
  31. }
  32. private System.Collections.IEnumerator DownloadFile(DownLoadMaterial downloadMaterial)
  33. {
  34. bool isDownLoad;
  35. Debug.Log("Hjj DownloadFile :" + downloadMaterial.localLoadPath + " " + downloadMaterial.type);
  36. using (UnityWebRequest webRequest = new UnityWebRequest(downloadMaterial.localLoadPath, UnityWebRequest.kHttpVerbGET))
  37. {
  38. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  39. webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
  40. webRequest.SetRequestHeader("Content-Type", "application/json");
  41. Debug.Log("Hjj DownloadFile start:" + downloadMaterial.localLoadPath + " ");
  42. yield return webRequest.SendWebRequest();
  43. Debug.Log("Hjj DownloadFile end:" + downloadMaterial.localLoadPath + " ");
  44. while (!webRequest.isDone)
  45. {
  46. // 此处可以显示下载进度条等UI操作
  47. //progress = webRequest.downloadProgress;
  48. //onProgress?.Invoke(progress);
  49. // Debug.Log("Download Progress: " + progress);
  50. yield return new WaitForFixedUpdate();
  51. }
  52. if (webRequest.isHttpError || webRequest.isNetworkError)
  53. {
  54. Debug.LogError("Hjj DownloadFile end:" + downloadMaterial.localLoadPath + "Download Failed: " + webRequest.error);
  55. // 下载失败
  56. //downLoadState = webRequest.error;
  57. isDownLoad = false;
  58. MsgHandler.SendMsg(downloadMaterial.downLoadPath, new Msg(downloadMaterial.downLoadPath, null));
  59. }
  60. else
  61. {
  62. Debug.Log("Hjj DownloadFile isDone:"+webRequest.isDone + " " + webRequest.downloadHandler.data.Length);
  63. // Debug.LogError("Download Failed: " + webRequest.error);
  64. dicLoaclData.Add(downloadMaterial.downLoadPath, webRequest.downloadHandler.data);
  65. isDownLoad = true;
  66. MsgHandler.SendMsg(downloadMaterial.downLoadPath, new Msg(downloadMaterial.downLoadPath, webRequest.downloadHandler.data));
  67. }
  68. }
  69. }
  70. /// <summary>
  71. /// 读取本地数据
  72. /// </summary>
  73. private void ReadLocaData(DownLoadMaterial downloadMaterial)
  74. {
  75. }
  76. IEnumerator DownloadFileIO(DownLoadMaterial downloadMaterial)
  77. {
  78. if (!File.Exists(downloadMaterial.localLoadPath))
  79. {
  80. Debug.Log("DGJ ====》 文件不存在 " + downloadMaterial.localLoadPath);
  81. MsgHandler.SendMsg(downloadMaterial.downLoadPath, new Msg(downloadMaterial.downLoadPath, null));
  82. }
  83. else
  84. {
  85. using (FileStream fs = new FileStream(downloadMaterial.localLoadPath, FileMode.Open))
  86. {
  87. long fileSize = new FileInfo(downloadMaterial.localLoadPath).Length;
  88. byte[] buffer = new byte[fileSize]; // 设置一个读取缓冲区
  89. int bytesRead;
  90. while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
  91. {
  92. // 在这里处理读取到的数据
  93. // Console.WriteLine("Read " + bytesRead + " bytes.");
  94. }
  95. yield return bytesRead == 0;
  96. dicLoaclData.Add(downloadMaterial.downLoadPath, buffer);
  97. // isDownLoad = true;
  98. MsgHandler.SendMsg(downloadMaterial.downLoadPath, new Msg(downloadMaterial.downLoadPath, buffer));
  99. }
  100. }
  101. }
  102. }