using System.Collections; using System.Collections.Generic; using UnityEngine; using QFramework; using System.IO; using Newtonsoft.Json; using UnityEngine.Networking; public class LoadManager : MonoSingleton { /// /// 本次运行已经加载过的物体 /// private List listMaterialData; /// /// 下载过的物体 /// // private List listLoadData; private List listDownLoad; private bool isLoading; public bool isSave = false; private void Start() { // PlayerPrefs.DeleteAll(); // PlayerPrefs.DeleteAll(); isLoading = true; listMaterialData = new List(); listDownLoad = new List(); if (PlayerPrefs.HasKey("LoadData111")) { string str = PlayerPrefs.GetString("LoadData11"); listDownLoad = JsonConvert.DeserializeObject>(str); if (listDownLoad == null) listDownLoad = new List(); Debug.Log(listDownLoad.Count); for (int i = 0; i < listDownLoad.Count; i++) { Debug.Log(listDownLoad[i].mObj.name + " " + listDownLoad[i].updateTime); } } else { listDownLoad = new List(); } } private void Update() { if(isSave) { isSave = false; SaveData(); } } /// /// 初始 下载所有需要的素材 /// /// public void InitLoad(List listObjValue) { bool state; //int num; Debug.Log("Init AllDownLoad " + listObjValue.Count); try { // 初始化 for (int i = 0; i < listObjValue.Count; i++) { state = true; if (listDownLoad != null) for (int j = 0; j < listDownLoad.Count; j++) { // Debug.Log(listObjValue[i].mObj.DownloadPath + " NAME " + listDownLoad[j].mObj.DownloadPath); if (listDownLoad[j].mObj.DownloadPath == listObjValue[i].mObj.DownloadPath && listDownLoad[j].updateTime == listObjValue[i].updateTime) { Debug.Log(listObjValue[i].mObj.name+" False "); state = false; break; } } if (state) { if (listObjValue[i].mObj.DownloadPath.IsNotNullAndEmpty() && listObjValue[i].mObj.type != "4") { DownLoadManage.Instance.AddDownLoadData(listObjValue[i]); isLoading = true; } } } LoadShow.Instance.allDownLoad = listObjValue.Count; LoadShow.Instance.ViewShow(true); Debug.Log("AllDownLoad " + DownLoadManage.Instance.AllDownLoad); if (DownLoadManage.Instance.AllDownLoad == 0) { Debug.Log("AllDownLoad " + DownLoadManage.Instance.AllDownLoad); GameManager.Instance.state = true; } } catch (System.Exception) { ErrorLogPanel.Instance.Show(" 初始化下载信息出现错误 "); } } /// /// 加载素材 /// /// /// public void LoadMaterial(MaterialObjValue value , int updateTime) { try { for (int i = 0; i < listMaterialData.Count; i++) { // 已经加载过 if (listMaterialData[i].mObj.DownloadPath == value.DownloadPath) { SendMsg(value.DownloadPath, listMaterialData[i].value); return; } } Debug.Log(listDownLoad.Count); for (int i = 0; i < listDownLoad.Count; i++) { // 已经下载了,无需更新,直接加载本地资源 if (listDownLoad[i].mObj.DownloadPath == value.DownloadPath && listDownLoad[i].updateTime == updateTime) { object obj; switch ((MaterialType)int.Parse(value.type)) { case MaterialType.Video: string path = listDownLoad[i].mObj.localSavePath; obj = path; SendMsg(value.DownloadPath, obj); listMaterialData.Add(new MaterialData(value, value.localSavePath)); break; case MaterialType.Model: DownLoadManage.Instance.AddLocalData(new DownLoadMaterial(value, updateTime)); break; case MaterialType.Image: DownLoadManage.Instance.AddLocalData(new DownLoadMaterial(value, updateTime)); break; default: break; } return; } } // 需要下载 DownLoadManage.Instance.AddDownLoadData(new DownLoadMaterial(value, updateTime)); } catch (System.Exception e) { ErrorLogPanel.Instance.Show(" 加载素材出现错误 " + e.Message); throw; } } /// /// 发送下载消息 /// /// /// public void SendMsg(string name, object value) { MsgHandler.SendMsg(name, new Msg("", value)); } /// /// 下载结束 /// /// /// public void DownLoadEnd( DownLoadMaterial mObj,object value ) { // Debug.Log(listMaterialData.Count +" ++++++++++++++"); listDownLoad.Add(mObj); // SaveData(); isSave = true; listMaterialData.Add(new MaterialData(mObj.mObj, value)); SendMsg(mObj.mObj.DownloadPath, value); // Debug.Log(mObj.mObj.name); // 后续根据实际测试结果来决定是否需要保存 模型的object文件 (可能会造成内存占用过大的问题) } /// /// 获取已下载素材 /// /// /// /// public object GetMaterial(string downLoadPath , long updatetime) { // Debug.Log(listMaterialData.Count); for (int i = 0; i < listMaterialData.Count; i++) { // Debug.Log(listMaterialData[i].mObj.DownloadPath +" "+downLoadPath); if(listMaterialData[i].mObj.DownloadPath == downLoadPath) { return listMaterialData[i].value; } } return null; } /// /// 初始加载结束 /// public void DownLoadFinish() { if (isLoading) isLoading = false; // Debug.Log("DownLoadFinish @@@@@@@@@@@@@@@@@@@@@@@"); GameManager.Instance.state = true; } protected void OnDestroy() { SaveData(); } /// /// 保存数据至本地 离线版 /// private void SaveData() { string str = JsonConvert.SerializeObject(listDownLoad); PlayerPrefs.SetString("LoadData111", str); } } public class DownLoadMaterial { public MaterialObjValue mObj { get; set; } public int updateTime { get; set; } public DownLoadMaterial(MaterialObjValue mObj, int updateTime) { this.mObj = mObj; this.updateTime = updateTime; } } public class MaterialData { public MaterialObjValue mObj; public object value; public MaterialData(MaterialObjValue mObj, object value) { this.mObj = mObj; this.value = value; } }