using System.Collections; using System.Collections.Generic; using UnityEngine; using QFramework; using System; using LitJson; using System.IO; public class DownLoadManage : MonoSingleton { /// /// 最大下载数 /// public int MaxDownLoad = 2; /// /// 当前下载数 /// public int NowDownLoad = 0; /// /// 所有待下载数 /// public int AllDownLoad = 0; private bool dlState = true; private string methodName; /// /// 待下载列表 /// private List listMaterialData; /// /// 当前正在下载列表 /// private List listDownLoading; /// /// 下载本地素材列表 /// private List listLocalMaterialData; private void Start() { listMaterialData = new List(); listDownLoading = new List(); listLocalMaterialData = new List(); StartCoroutine(DownLoadControl()); } IEnumerator DownLoadControl() { while (true) { // Debug.Log(NowDownLoad + " Load " + MaxDownLoad); if( NowDownLoad < MaxDownLoad) { if (listMaterialData.Count > 0) { methodName = "/file/download"; //if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty()) // methodName = "/cdn/download"; NowDownLoad++; Debug.Log("@@@@@@@" + listMaterialData[0].mObj.name + " " + listMaterialData[0].mObj.type + " " + listMaterialData[0].mObj.DownloadPath + " " + listMaterialData[0].mObj.localSavePath); switch ((MaterialType)int.Parse(listMaterialData[0].mObj.type)) { case MaterialType.Text: //HttpTool.Instance.PostTxt(listMaterialData[0], listMaterialData[0].mObj.DownloadPath, "", // CallBack); Debug.Log("TEST"); break; case MaterialType.Image: SendMaterials sendImage = new SendMaterials(); sendImage.url = listMaterialData[0].mObj.DownloadPath; if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty()) sendImage.url = listMaterialData[0].mObj.cdnUrl; string jsonImage = JsonMapper.ToJson(sendImage); // Debug.Log("@@@@@@@" + jsonImage); if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty()) HttpTool.Instance.PostCDNImage(listMaterialData[0], jsonImage, CallBack); else HttpTool.Instance.PostImage(listMaterialData[0], methodName, jsonImage, CallBack); break; case MaterialType.Video: SendMaterials sendVideo = new SendMaterials(); sendVideo.url = listMaterialData[0].mObj.DownloadPath; if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty()) sendVideo.url = listMaterialData[0].mObj.cdnUrl; string jsonVideo = JsonMapper.ToJson(sendVideo); if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty()) HttpTool.Instance.PostCDNVideo(listMaterialData[0], jsonVideo, CallBack); else HttpTool.Instance.PostVideo(listMaterialData[0], methodName, jsonVideo, CallBack); break; case MaterialType.Model: if (Path.GetFileName(listMaterialData[0].mObj.DownloadPath).Contains(".")) { ErrorLogPanel.Instance.Show(" 上传的模型文件格式不正确 " + Path.GetFileName(listMaterialData[0].mObj.DownloadPath)); } Debug.Log(listMaterialData[0].mObj.cdnUrl); if(listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty()) if(Path.GetFileName(listMaterialData[0].mObj.cdnUrl).Contains(".")) { ErrorLogPanel.Instance.Show(" 上传的模型文件格式不正确 " + Path.GetFileName(listMaterialData[0].mObj.DownloadPath)); } SendMaterials sendModel = new SendMaterials(); sendModel.url = listMaterialData[0].mObj.DownloadPath; if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty()) sendModel.url = listMaterialData[0].mObj.cdnUrl; string jsonModel = JsonMapper.ToJson(sendModel); if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty()) HttpTool.Instance.PostCDNBundle(listMaterialData[0], listMaterialData[0].updateTime, jsonModel, CallBack); else HttpTool.Instance.PostBundle(listMaterialData[0], methodName, listMaterialData[0].updateTime, jsonModel, CallBack); break; default: break; } // 下载 listDownLoading.Add(listMaterialData[0]); listMaterialData.Remove(listMaterialData[0]); AllDownLoad--; } if (listLocalMaterialData.Count > 0) { methodName = "/file/download"; //if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty()) // methodName = "/cdn/download"; NowDownLoad++; switch ((MaterialType)int.Parse(listLocalMaterialData[0].mObj.type)) { case MaterialType.NULL: break; case MaterialType.Image: HttpTool.Instance.GetLocalSprite(listLocalMaterialData[0], CallBack); break; case MaterialType.Model: //SendMaterials sendModel = new SendMaterials(); //sendModel.url = listMaterialData[0].mObj.DownloadPath; //if (listMaterialData[0].mObj.cdnUrl.IsNotNullAndEmpty()) // sendModel.url = listMaterialData[0].mObj.cdnUrl; //string jsonModel = JsonMapper.ToJson(sendModel); //HttpTool.Instance.PostBundle(listLocalMaterialData[0], methodName, listLocalMaterialData[0].updateTime, jsonModel, CallBack); HttpTool.Instance.GetLocalModel(listLocalMaterialData[0], CallBack); break; default: break; } GameManager.Instance.text.text = AllDownLoad.ToString(); listDownLoading.Add(listLocalMaterialData[0]); listLocalMaterialData.Remove(listLocalMaterialData[0]); AllDownLoad--; } } yield return new WaitForSeconds(0.5f); } } /// /// 添加下载数据 /// /// public void AddDownLoadData(DownLoadMaterial value) { // Debug.Log("添加本地下载数据 " + value.mObj.name); listMaterialData.Add(value); AllDownLoad++; } /// /// 添加本地下载数据 /// /// public void AddLocalData(DownLoadMaterial value) { // Debug.Log("添加本地下载数据"); listLocalMaterialData.Add(value); AllDownLoad++; } public void CallBack(DownLoadMaterial mObj, object value) { listDownLoading.Remove(mObj); NowDownLoad--; // Debug.Log(AllDownLoad + " AllDownLoad"); if (AllDownLoad < 1) LoadManager.Instance.DownLoadFinish(); // 通知 下载完成 LoadManager.Instance.DownLoadEnd(mObj, value); } }