12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using UnityEngine.Networking;
- using static DownLoadXRManager;
- public class DownLoadURLXRItem : MonoBehaviour
- {
- public static Dictionary<string, List<DownLoadUrlConfig>> downLoadingList = new Dictionary<string, List<DownLoadUrlConfig>>();
- public void startDownload(DownLoadUrlConfig config)
- {
- StartCoroutine(ReadStreamingAssetsFile(config));
- }
- IEnumerator ReadStreamingAssetsFile(DownLoadUrlConfig config)
- {
- if (DownLoadXRManager.downLoadCache.ContainsKey(config.url))
- {
- config.bytes?.Invoke(DownLoadXRManager.downLoadCache[config.url]);
- yield return null;
- }
- else
- {
- if(downLoadingList.ContainsKey(config.url))
- {
- downLoadingList[config.url].Add(config);
- }
- else
- {
- downLoadingList.Add(config.url,new List<DownLoadUrlConfig>());
- downLoadingList[config.url].Add(config);
- string filePath = config.url;
- Debug.Log("×¼±¸ÏÂÔØ" + filePath);
- UnityWebRequest www = UnityWebRequest.Get(filePath);
- www.SendWebRequest();
- while (!www.isDone)
- {
- for (int i = 0; i <downLoadingList[config.url].Count; i++)
- {
- downLoadingList[config.url][i].presson?.Invoke(www.downloadProgress);
- }
- yield return null;
- }
- if (www.result != UnityWebRequest.Result.ConnectionError && www.isDone)
- {
- byte[] bytes = www.downloadHandler.data;
- for (int i = 0; i < downLoadingList[config.url].Count; i++)
- {
- downLoadingList[config.url][i].bytes?.Invoke(bytes);
- }
- if (!DownLoadXRManager.downLoadCache.ContainsKey(config.url))
- DownLoadXRManager.downLoadCache.Add(config.url, bytes);
- downLoadingList.Remove(config.url);
- }
- else
- {
- for (int i = 0; i < downLoadingList[config.url].Count; i++)
- {
- downLoadingList[config.url][i].bytes?.Invoke(null);
- }
- downLoadingList.Remove(config.url);
- }
- }
- }
- Destroy(this.gameObject);
- }
- }
|