using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using XRTool.Util; public class LoadManager : Singleton { 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>> onProgresslist = new Dictionary>>(); public Dictionary>> callbacklist = new Dictionary>>(); public void setaction(string id,Action onProgress, Action callback) { if(onProgresslist.ContainsKey(id)) { onProgresslist[id].Add(onProgress); } else { List> f = new List>(); f.Add(onProgress); onProgresslist.Add(id, f); } if (callbacklist.ContainsKey(id)) { callbacklist[id].Add(callback); } else { List < Action > c = new List>(); c.Add(callback); callbacklist.Add(id, c); } } public Dictionary downList = new Dictionary(); public void load(ModelItem model, Action onProgress, Action 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(); 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 xmlcallback, Action 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(); 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(); 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 downloadQueueList = new Queue(); }