using System.Collections; using System.Collections.Generic; using System.IO; using Thousandto.Cfg.Data; using Thousandto.Code.Center; using Thousandto.Core.Asset; using Thousandto.Core.Base; using Thousandto.Update.Flow; using Thousandto.Update.Manager; using UnityEngine; using PathUtils = UnityEngine.Gonbest.MagicCube.PathUtils; using CoroutinePool = UnityEngine.Gonbest.MagicCube.CoroutinePool; using StringUtils = UnityEngine.Gonbest.MagicCube.StringUtils; namespace Thousandto.Code.Logic { /// /// 后台加载场景 /// public class SceneBackLoadSystem { #region//私有变量 private int _timeOut = 1800; private int _tickTime = 0; private bool _isRun = false; private SceneBackLoadData _curData = null; private List _dataList = null; #endregion #region//const private const string CONST_PATH = "GameAssets/Resources/Scene"; #endregion #region//公共函数 public void Initialize() { _dataList = new List(DeclareSceneLoadConfig.CacheDataCount); var enumer = DeclareSceneLoadConfig.CacheData.GetEnumerator(); try { while (enumer.MoveNext()) { var data = new SceneBackLoadData(enumer.Current.Value); _dataList.Add(data); } } finally { enumer.Dispose(); } } public void Uninitialize() { _isRun = false; _dataList.Clear(); } //后台加载场景 public void BackDown() { if (_isRun) return; int total = GameCenter.UpdateSystem.GetBaseResTotalSize(); int downloaded = GameCenter.UpdateSystem.GetTotalDownloadedSize(); if (total <= 0 && downloaded >= total) { //没有资源需要下载了 return; } CoroutinePool.AddTask(DownLoad()); _isRun = true; } #endregion #region//私有函数 private void Download(string requestPath) { _tickTime = _timeOut; //这里把请求路径中统一分隔符 var fc = new FileClasses(requestPath.Replace('\\', '/'), AssetTypeCode.Scene, null, AsyncActionType.LoadScene, false, null,FileRequestTypeCode.WWW); if (PathUtils.IsStreaming() && fc.FileRequestCode != FileRequestTypeCode.Resource) { //FLogger.LogTime(string.Format("++++++++++++开始下载场景的资源的请求路径是:{0} ", requestPath)); //下载场景文件 UpdateManager.Instance.DownloadResource(fc.AssetsPath, OnDownloadCallback, fc); } else { _isRun = false; } } IEnumerator DownLoad() { while (GetDataCount()>0) { _curData = GetData(); if (!_curData.IsLoading && !UpdateManager.Instance.IsPaused()) { //string assetPath = string.Format("{0}/{1}", CONST_PATH, data.Cfg.ResName); string requstPath = StringUtils.CombineString(AssetConstDefine.PathScene, _curData.Cfg.ResName, AssetConstDefine.ExtUnity); _curData.IsLoading = true; Download(requstPath); while (_curData.Result == SceneDownLoadResult.Defalut || !_curData.IsCacheOver) { yield return new WaitForEndOfFrame(); } _curData.IsLoading = false; if (_curData.Result == SceneDownLoadResult.Success) { DownLoadSucess(_curData); } else if (_curData.Result == SceneDownLoadResult.Fail) { //FLogger.LogTime(string.Format("++++++++++++场景:{0}====下载失败! ", _curData.Cfg.Name)); } } yield return new WaitForSeconds(1); } } private void OnDownloadCallback(string path, int result, object obj) { FileClasses fc = obj as FileClasses; fc.AssetsPath = path; fc.ErrorCode = result; string name = string.Empty; string[] strs = path.Split('/'); for (int i = 0; i < strs.Length; i++) { if (strs[i].Contains(AssetConstDefine.ExtUnity3d)) { name = strs[i].Replace(AssetConstDefine.ExtUnity3d,""); } } //FLogger.LogTime(string.Format("++++++++++++场景资源下载结果回调:{0} ", name)); if (_curData != null) { if (fc.ErrorCode == CodeDefine.RET_BACKDOWNLOAD_SUCCESS) { //下载成功了 可以进行场景缓存 _curData.Result = SceneDownLoadResult.Success; //GameCenter.CacheSceneSystem.PreLoadSceneForCache(path, (ret)=> { // data.IsCacheOver = true; //}); _curData.IsCacheOver = true; //FLogger.LogTime("场景资源后台下载成功了!"); } else if (fc.ErrorCode == CodeDefine.RET_BACKDOWNLOAD_ALREADYEXIST) { _curData.Result = SceneDownLoadResult.Success; _curData.IsCacheOver = true; //FLogger.LogTime("场景资源后台下载 资源已经存在!"); } else { //下载失败或者下载暂停了 _curData.Result = SceneDownLoadResult.Fail; _curData.IsCacheOver = true; //FLogger.LogTime("场景资源后台下载失败了!"); } } } private SceneBackLoadData GetData() { SceneBackLoadData data = null; if (_dataList.Count > 0) { data = _dataList[0]; } return data; } private int GetDataCount() { return _dataList.Count; } private SceneBackLoadData GetDataByName(string name) { SceneBackLoadData data = null; for (int i = _dataList.Count - 1; i >= 0; i--) { if (name == _dataList[i].Cfg.ResName) { data = _dataList[i]; } } return data; } private void DownLoadSucess(SceneBackLoadData data) { for (int i = _dataList.Count - 1; i >= 0; i--) { if (_dataList[i].Cfg.ResName == data.Cfg.ResName) { _dataList.RemoveAt(i); } } //FLogger.LogTime(string.Format("++++++++++++场景:{0}====下载成功! ", data.Cfg.Name)); } #endregion } }