DownloadPointFile.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Collections;
  2. using System.IO;
  3. using Blue;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. using UnityEngine;
  8. using UnityEngine.Networking;
  9. public class DownloadPointFile : AbstractController
  10. {
  11. private UnityWebRequest webRequestPointByte;
  12. private UnityWebRequest webRequestPointJson;
  13. private void Awake()
  14. {
  15. this.SubscribeEvent<DownLoadMapFileEvent>(Download).UnSubScribeWhenGameObjectDestroyed(gameObject);
  16. }
  17. private void Download(DownLoadMapFileEvent e)
  18. {
  19. if (e.FileType == "json")
  20. StartCoroutine(DownLoadFile(webRequestPointJson, e.Url, "." + e.FileType, e.ProjectID.ToString()));
  21. else
  22. StartCoroutine(DownLoadFile(webRequestPointByte, e.Url, "." + e.FileType, e.ProjectID.ToString()));
  23. }
  24. private IEnumerator DownLoadFile(UnityWebRequest webRequestPoint, string downloadingUrl, string fileType, string projectId)
  25. {
  26. string fileName = this.GetUtility<GetFileNameUtility>().GetFileName(downloadingUrl, fileType);
  27. if (!PlayerPrefs.HasKey(projectId+ fileType) ||
  28. PlayerPrefs.GetString(projectId+ fileType) != fileName||
  29. !File.Exists(Application.persistentDataPath + "/Map Data/" + fileName))
  30. {
  31. PlayerPrefs.SetString(projectId+ fileType, fileName);
  32. Debug.LogError("文件不存在,下载文件");
  33. //发送请求
  34. webRequestPoint = UnityWebRequest.Get(downloadingUrl);
  35. webRequestPoint.timeout = 30;//设置超时,若webRequest.SendWebRequest()连接超时会返回,且isNetworkError为true
  36. yield return webRequestPoint.SendWebRequest();
  37. if (webRequestPoint.result == UnityWebRequest.Result.ConnectionError) Debug.Log("Download Point Cloud Error:" + webRequestPoint.error);
  38. else
  39. {
  40. if (Directory.Exists(Application.persistentDataPath + "/Map Data") == false)
  41. Directory.CreateDirectory(Application.persistentDataPath + "/Map Data");
  42. if (System.IO.File.Exists(Application.persistentDataPath + "/Map Data/" + fileName))
  43. System.IO.File.Delete(Application.persistentDataPath + "/Map Data/" + fileName);
  44. //获取二进制数据
  45. var File = webRequestPoint.downloadHandler.data;
  46. //创建文件写入对象
  47. FileStream nFile = new FileStream(Application.persistentDataPath + "/Map Data/" + fileName, FileMode.Create);
  48. //写入数据
  49. nFile.Write(File, 0, File.Length);
  50. //关闭当前流并释放与之相关联的所有系统资源
  51. nFile.Close(); nFile.Dispose(); nFile = null;
  52. if (fileType == ".bytes")
  53. {
  54. #if UNITY_EDITOR
  55. AssetDatabase.Refresh();
  56. #endif
  57. this.SendCommand(new LoadMapFileCommand(fileName) { });
  58. }
  59. }
  60. }
  61. else
  62. {
  63. Debug.LogError("文件已存在");
  64. if (fileType == ".bytes")
  65. {
  66. this.SendCommand(new LoadMapFileCommand(fileName) { });
  67. }
  68. }
  69. }
  70. /// <summary>
  71. /// 获取下载进度
  72. /// </summary>
  73. public float GetProcess(UnityWebRequest webRequestPoint)
  74. {
  75. if (webRequestPoint!=null)
  76. {
  77. return webRequestPoint.downloadProgress;
  78. }
  79. return 0;
  80. }
  81. /// <summary>
  82. /// 获取当前下载内容长度
  83. /// </summary>
  84. public long GetCurrentLength(UnityWebRequest webRequestPoint)
  85. {
  86. if (webRequestPoint != null)
  87. {
  88. return (long)webRequestPoint.downloadedBytes;
  89. }
  90. return 0;
  91. }
  92. }