1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System.Collections;
- using System.IO;
- using Blue;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- using UnityEngine;
- using UnityEngine.Networking;
- public class DownloadPointFile : AbstractController
- {
- private UnityWebRequest webRequestPointByte;
- private UnityWebRequest webRequestPointJson;
- private void Awake()
- {
- this.SubscribeEvent<DownLoadMapFileEvent>(Download).UnSubScribeWhenGameObjectDestroyed(gameObject);
- }
- private void Download(DownLoadMapFileEvent e)
- {
- if (e.FileType == "json")
- StartCoroutine(DownLoadFile(webRequestPointJson, e.Url, "." + e.FileType, e.ProjectID.ToString()));
- else
- StartCoroutine(DownLoadFile(webRequestPointByte, e.Url, "." + e.FileType, e.ProjectID.ToString()));
- }
- private IEnumerator DownLoadFile(UnityWebRequest webRequestPoint, string downloadingUrl, string fileType, string projectId)
- {
- string fileName = this.GetUtility<GetFileNameUtility>().GetFileName(downloadingUrl, fileType);
- if (!PlayerPrefs.HasKey(projectId+ fileType) ||
- PlayerPrefs.GetString(projectId+ fileType) != fileName||
- !File.Exists(Application.persistentDataPath + "/Map Data/" + fileName))
- {
- PlayerPrefs.SetString(projectId+ fileType, fileName);
- Debug.LogError("文件不存在,下载文件");
- //发送请求
- webRequestPoint = UnityWebRequest.Get(downloadingUrl);
- webRequestPoint.timeout = 30;//设置超时,若webRequest.SendWebRequest()连接超时会返回,且isNetworkError为true
- yield return webRequestPoint.SendWebRequest();
- if (webRequestPoint.result == UnityWebRequest.Result.ConnectionError) Debug.Log("Download Point Cloud Error:" + webRequestPoint.error);
- else
- {
- if (Directory.Exists(Application.persistentDataPath + "/Map Data") == false)
- Directory.CreateDirectory(Application.persistentDataPath + "/Map Data");
- if (System.IO.File.Exists(Application.persistentDataPath + "/Map Data/" + fileName))
- System.IO.File.Delete(Application.persistentDataPath + "/Map Data/" + fileName);
- //获取二进制数据
- var File = webRequestPoint.downloadHandler.data;
- //创建文件写入对象
- FileStream nFile = new FileStream(Application.persistentDataPath + "/Map Data/" + fileName, FileMode.Create);
- //写入数据
- nFile.Write(File, 0, File.Length);
- //关闭当前流并释放与之相关联的所有系统资源
- nFile.Close(); nFile.Dispose(); nFile = null;
- if (fileType == ".bytes")
- {
- #if UNITY_EDITOR
- AssetDatabase.Refresh();
- #endif
- this.SendCommand(new LoadMapFileCommand(fileName) { });
- }
- }
- }
- else
- {
- Debug.LogError("文件已存在");
- if (fileType == ".bytes")
- {
- this.SendCommand(new LoadMapFileCommand(fileName) { });
- }
-
- }
- }
- /// <summary>
- /// 获取下载进度
- /// </summary>
- public float GetProcess(UnityWebRequest webRequestPoint)
- {
- if (webRequestPoint!=null)
- {
- return webRequestPoint.downloadProgress;
- }
- return 0;
- }
- /// <summary>
- /// 获取当前下载内容长度
- /// </summary>
- public long GetCurrentLength(UnityWebRequest webRequestPoint)
- {
- if (webRequestPoint != null)
- {
- return (long)webRequestPoint.downloadedBytes;
- }
- return 0;
- }
- }
|