123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using XRTool.Util;
- public class LoadManager : Singleton<LoadManager>
- {
- const int maxLoad = 3;
- int nowLoad = 0;
- public LoadManager()
- {
- TimerMgr.Instance.CreateTimer(() => {
- if (downloadQueueList.Count>0&& nowLoad< maxLoad)
- {
- int ct = downloadQueueList.Count;
- // Debug.LogError(" downloadQueueList =====> " + ct);
- for (int i = 0; i < ct; i++)
- {
- DownLoadItem item = downloadQueueList.Dequeue();
- item.onProgress += (float f) => {
- Debug.Log("下载进度===>" + f);
- };
- item.callback += (bool b) => {
- Debug.Log("下载完成===>" + nowLoad);
- nowLoad--;
- };
- // GameScene.Instance.StartCoroutine( item.DownloadFile());
- item.DownloadFileMsg();
- nowLoad++;
- if(nowLoad>= maxLoad)
- {
- break;
- }
- }
- }
-
-
- }, 1,-1);
- }
- public Dictionary<string, List<Action<float>>> onProgresslist = new Dictionary<string, List<Action<float>>>();
- public Dictionary<string, List<Action<byte[]>>> callbacklist = new Dictionary<string, List<Action<byte[]>>>();
- public void setaction(string id,Action<float> onProgress, Action<byte[]> callback)
- {
- if(onProgresslist.ContainsKey(id))
- {
- onProgresslist[id].Add(onProgress);
- }
- else
- {
- List<Action<float>> f = new List<Action<float>>();
- f.Add(onProgress);
- onProgresslist.Add(id, f);
- }
- if (callbacklist.ContainsKey(id))
- {
- callbacklist[id].Add(callback);
- }
- else
- {
- List < Action <byte[]>> c = new List<Action<byte[]>>();
- c.Add(callback);
- callbacklist.Add(id, c);
- }
- }
- public Dictionary<string, DownLoadItem> downList = new Dictionary<string, DownLoadItem>();
- public void load(ModelItem model, Action<float> onProgress, Action<byte[]> callback)
- {
- string dName = getLoadName(model.url, model.Version);
- Debug.Log("dName ======> " + dName+ " model.url==>"+ model.url);
- if (!downList.ContainsKey(dName))
- {
- GameObject obj = new GameObject(dName);
- DownLoadItem dli = obj.AddComponent<DownLoadItem>();
- dli.Init(model.url, model.uid ,model.modelType,model.updateTime);
- downList.Add(dName, dli);
- }
- setaction(dName, onProgress, callback);
- downList[dName].onProgress += (float f) => {
- for (int i = 0; i < onProgresslist[dName].Count; i++)
- {
- onProgresslist[dName][i].Invoke(f);
- }
- };
- downList[dName].callback += (bool b) => {
- if (b)
- {
- if (!downList[dName].isAction)
- {
- //涓嬭浇鎴愬姛骞舵棤澶勭悊
- switch (model.modelType)
- {
- case ModelType.Image:
- break;
- case ModelType.Video:
- break;
- case ModelType.ABModel:
- break;
- }
- if(downList[dName].downLoadData!=null)
- {
- Debug.Log(" LoadManager " + dName + " Length : " + downList[dName].downLoadData.Length);
- model.SetData(downList[dName].downLoadData, downList[dName].data);
- }else if(model.prefabModel==null)
- {
- model.prefabModel = new GameObject("空物体"+ downList[dName].id);
- // model.initFrist();
- }
- for (int i = 0; i < callbacklist[dName].Count; i++)
- {
- callbacklist[dName][i].Invoke(downList[dName].downLoadData);
- }
- }
- }
- else
- {
- //涓嬭浇澶辫触
- }
- };
- }
- public GameObject datFileObj;
- public GameObject xmlFileDownObj;
- public void loadVuforia(string xmlFile, string datFile, Action<bool> xmlcallback, Action<bool> datcallback)
- {
- if (File.Exists(Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia.xml"))
- {
- File.Delete(Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia.xml");
- }
- if (File.Exists(Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia.dat"))
- {
- File.Delete(Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia.dat");
- }
- if (!Directory.Exists(Application.persistentDataPath + "/StreamingAssets"))
- {
- Directory.CreateDirectory(Application.persistentDataPath + "/StreamingAssets");
- if(!Directory.Exists(Application.persistentDataPath + "/StreamingAssets/Vuforia"))
- {
- Directory.CreateDirectory(Application.persistentDataPath + "/StreamingAssets/Vuforia");
- }
- }
- xmlFileDownObj = new GameObject("VufroiaTriggerDownLoad_xmlFile");
- DownLoadItem xmlFileItem = xmlFileDownObj.AddComponent<DownLoadItem>();
- xmlFileItem.Init(xmlFile, "VufroiaTriggerDownLoad_xmlFile",ModelType.vuforial, long.Parse(GameManager.Instance.m_scene.updateTime));
- xmlFileItem.onProgress += (float f) => {
- };
- xmlFileItem.callback += (bool b) =>
- {
- Debug.Log("Vuforia 下载完成");
- if (b)
- {
- Debug.Log("Vuforia 下载完成 ===>" + VufroiaTrigger.LoacldatFile);
- // File.WriteAllBytes(VufroiaTrigger.LoaclxmlFile, xmlFileItem.downLoadData);
- }
- xmlcallback.Invoke(b);
- };
- datFileObj = new GameObject("VufroiaTriggerDownLoad_datFile");
- DownLoadItem datFileItem = datFileObj.AddComponent<DownLoadItem>();
- datFileItem.Init(datFile, "VufroiaTriggerDownLoad_xmlFile",ModelType.vuforial, long.Parse(GameManager.Instance.m_scene.updateTime));
- datFileItem.onProgress += (float f) => {
- };
- datFileItem.callback += (bool b) =>
- {
- Debug.Log("Vuforia 下载完成");
- if (b)
- {
- Debug.Log("Vuforia 下载完成 ===>"+ VufroiaTrigger.LoacldatFile);
- // File.WriteAllBytes(VufroiaTrigger.LoacldatFile, datFileItem.downLoadData);
- }
- datcallback.Invoke(b);
- };
- }
- public string getLoadName(string url, string version)
- {
- return url + "_" + version;
- }
- public DownLoadItem getdownloaditem(ModelItem mi)
- {
- Debug.Log("getdownloaditem=====>"+getLoadName(mi.url, mi.Version));
- if(downList.ContainsKey(getLoadName(mi.url, mi.Version)))
- {
- DownLoadItem dii = downList[getLoadName(mi.url, mi.Version)];
- return dii;
- }
- else
- {
- return null;
- }
-
- }
- public Queue<DownLoadItem> downloadQueueList = new Queue<DownLoadItem>();
- }
|