123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using QFramework;
- using System.IO;
- using Newtonsoft.Json;
- using UnityEngine.Networking;
- public class LoadManager : MonoSingleton<LoadManager>
- {
- /// <summary>
- /// 本次运行已经加载过的物体
- /// </summary>
- private List<MaterialData> listMaterialData;
- /// <summary>
- /// 下载过的物体
- /// </summary>
- // private List<MaterialObjValue> listLoadData;
- private List<DownLoadMaterial> listDownLoad;
- private bool isLoading;
- public bool isSave = false;
- private void Start()
- {
- // PlayerPrefs.DeleteAll();
- // PlayerPrefs.DeleteAll();
- isLoading = true;
- listMaterialData = new List<MaterialData>();
- listDownLoad = new List<DownLoadMaterial>();
- if (PlayerPrefs.HasKey("LoadData111"))
- {
- string str = PlayerPrefs.GetString("LoadData111");
- listDownLoad = JsonConvert.DeserializeObject<List<DownLoadMaterial>>(str);
- if (listDownLoad == null)
- listDownLoad = new List<DownLoadMaterial>();
- 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<DownLoadMaterial>();
- }
-
- }
- private void Update()
- {
- if(isSave)
- {
- isSave = false;
- SaveData();
- }
- }
- /// <summary>
- /// 初始 下载所有需要的素材
- /// </summary>
- /// <param name="listObjValue"></param>
- public void InitLoad(List<DownLoadMaterial> 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(" 初始化下载信息出现错误 ");
-
- }
-
- }
- /// <summary>
- /// 加载素材
- /// </summary>
- /// <param name="value"></param>
- /// <param name="updateTime"></param>
- 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;
- }
-
- }
- /// <summary>
- /// 发送下载消息
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- public void SendMsg(string name, object value)
- {
- MsgHandler.SendMsg(name, new Msg("", value));
- }
- /// <summary>
- /// 下载结束
- /// </summary>
- /// <param name="mObj"></param>
- /// <param name="value"></param>
- 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文件 (可能会造成内存占用过大的问题)
- }
-
-
- /// <summary>
- /// 获取已下载素材
- /// </summary>
- /// <param name="downLoadPath"></param>
- /// <param name="updatetime"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 初始加载结束
- /// </summary>
- public void DownLoadFinish()
- {
- if (isLoading)
- isLoading = false;
- // Debug.Log("DownLoadFinish @@@@@@@@@@@@@@@@@@@@@@@");
- GameManager.Instance.state = true;
- }
- protected void OnDestroy()
- {
- SaveData();
-
- }
- /// <summary>
- /// 保存数据至本地 离线版
- /// </summary>
- 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;
- }
- }
|