//清理所有 public void UnloadAllUsingAssets() { // _assetManager.CleanAllUsing(); // ResService.GetInstance().UnloadUnusedAssets(); }
public int BundleCount(JWObjList <string> resourceList) { ResPackConfig config = ResService.GetInstance().PackConfig; if (config == null) { return(0); } JWObjList <BundlePackInfo> bundles = GetBundleList(resourceList); if (bundles == null) { return(0); } int count = 0; for (int i = 0; i < bundles.Count; i++) { // 非NoBundle资源,或者在磁盘上有这个bundle BundlePackInfo bpi = bundles[i]; //if (!bpi.IsNoBundle()) //{ // count++; //} } return(count); }
/// <summary> /// 加载常驻bundle /// </summary> /// <param name="complete"></param> /// <param name="progress"></param> /// <returns></returns> public IEnumerator LoadResidentBundles(Action complete = null, BundleBatchLoadingDelegate progress = null) { ResPackConfig config = ResService.GetInstance().PackConfig; if (config == null) { JW.Common.Log.LogE("In LoadResidentBundles(), but ResPackConfig is null."); if (complete != null) { complete(); } yield break; } JWObjList <BundlePackInfo> bundleList = new JWObjList <BundlePackInfo>(); for (int i = 0; i < config.PackInfo.Count; i++) { BundlePackInfo p = config.PackInfo[i] as BundlePackInfo; if (p != null && p.Life == EBundleLife.Resident) { bundleList.Add(p); } } yield return(StartCoroutine(BundleService.GetInstance().BatchLoadAsync(bundleList, complete, progress))); }
private IEnumerator AsynchronousLoad_LoadResource() { while (true) { int find = 0; for (; find < _resourceRequesting.Count; find++) { if (_resourceRequesting[find].LoadBundleState != LoadStateLoading) { break; } } if (find >= _resourceRequesting.Count) { yield return(InstructionEnd); continue; } LoadData loadData = _resourceRequesting[find]; if (_assetManager.GetCacheCount(loadData.Data.Name) >= loadData.Data.Count) { _resourceRequesting.RemoveAt(find); if (loadData.Data.Callback != null) { loadData.Data.Callback.OnLoadAssetCompleted(loadData.Data.Name, AssetLoadResult.Success, null); yield return(null); } yield return(InstructionEnd); continue; } if (loadData.LoadBundleState == LoadStateFail) { JW.Common.Log.LogE("Loader.AsynchronousLoad_Resource : load bundle failed - {0}", loadData.Data.Name); _resourceRequesting.RemoveAt(find); if (loadData.Data.Callback != null) { loadData.Data.Callback.OnLoadAssetCompleted(loadData.Data.Name, AssetLoadResult.BundleFail, null); yield return(null); } yield return(InstructionEnd); continue; } loadData.Request = ResService.GetInstance().GetResourceAsync(loadData.Data.Filename); _resourceRequesting[find] = loadData; yield return(InstructionEnd); } }
/// <summary> /// 获取bundle打包信息 /// </summary> /// <param name="path">bundle路径</param> /// <returns></returns> BundlePackInfo GetPackInfo(string path) { ResPackConfig config = ResService.GetInstance().PackConfig; if (config == null) { return(null); } return(config.GetPackInfo(path) as BundlePackInfo); }
/// <summary> /// 根据资源列表取bundle列表 /// </summary> /// <param name="resourceList"></param> /// <param name="recordResources"></param> public JWObjList <BundlePackInfo> GetBundlePackInfoListForResources(JWObjList <string> resourceList, bool recordResources) { if (resourceList == null || resourceList.Count == 0) { return(null); } ResPackConfig config = ResService.GetInstance().PackConfig; if (config == null) { if (recordResources) { _unbundledResources.AddRange(resourceList); } return(null); } JWObjList <BundlePackInfo> bundleList = null; for (int i = 0; i < resourceList.Count; i++) { string path = resourceList[i]; BundlePackInfo info = config.GetPackInfoForResource(JW.Res.FileUtil.EraseExtension(path)) as BundlePackInfo; if (info != null) { if (bundleList == null) { bundleList = new JWObjList <BundlePackInfo>(); } if (!bundleList.Contains(info)) { bundleList.Add(info); } // bundle正在加载中的资源 if (recordResources && !_loadingResources.Contains(path)) { _loadingResources.Add(path); } } else { if (recordResources && !_unbundledResources.Contains(path)) { _unbundledResources.Add(path); } } } return(bundleList); }
/// <summary> /// 清理缓存的资源 /// </summary> /// <param name="uiStateHistory">UI状态历史列表(=null标识没有UI状态)</param> public void ClearUIStateCache(JWArrayList <string> uiStateHistory) { if (_loader == null) { return; } _tempList.Clear(); _tempList2.Clear(); if (uiStateHistory != null) { _assetManager.GetCacheUIState(ref _tempList); _loader.GetUIState(ref _tempList); for (int i = uiStateHistory.Count - 1; i >= 0; i--) { string stateName = uiStateHistory[i]; if (string.IsNullOrEmpty(stateName) || _tempList.IndexOf(stateName, StringComparer.OrdinalIgnoreCase) == -1) { continue; } _tempList2.Add(stateName); if (_tempList2.Count == CacheUIStateCount) { break; } } } _assetManager.ClearCache(_tempList2); _loader.ClearTask(_tempList2); if (AlwaysGc) { if (LuaGCHook != null) { LuaGCHook(true); } ResService.GetInstance().UnloadUnusedAssets(); return; } }
/// <summary> /// 初始化公共层 /// </summary> /// <param name="initialize">初始化//反初始化</param> public static void InitCommon(bool initialize) { if (initialize) { //统一随机数 UnityEngine.Random.InitState((int)DateTime.Now.Ticks); //开启日志 Log.GetInstance(); BundleService.GetInstance(); ResService.GetInstance(); //全局 DG.Tweening.DOTween.Init(true, true, DG.Tweening.LogBehaviour.ErrorsOnly); } else { ResService.DestroyInstance(); BundleService.DestroyInstance(); } }
/// 游戏更新检查后后启动 public void StartAfterUpdate(Action <bool> allCompletedCallback) { _allCompletedCallback = allCompletedCallback; #if USE_PACK_RES if (ResService.GetInstance().PackConfig == null) { JW.Common.Log.LogE("Preloader.StartAfterUpdate : resource initialize failed"); return; } // _bundleFiles = BundleMediator.GetInstance().GetPreloadBundles(); _bundleLoading = false; string filename = "main_shaders.ab"; _shaderBundle = BundleService.GetInstance().GetBundle(filename); if (_shaderBundle == null) { JW.Common.Log.LogE("Preloader.StartAfterUpdate : failed to get shader bundle"); } ResPackInfo pi = ResService.GetInstance().PackConfig.GetPackInfo(filename); if (pi == null) { _shaderFilename = new JWArrayList <string>(0); } else { _shaderFilename = new JWArrayList <string>(pi.Resources.Count); for (int i = 0; i < pi.Resources.Count; i++) { _shaderFilename.Add(pi.Resources[i].Path); } } #else _bundleFiles = new JWObjList <string>(0); _shaderBundle = null; _shaderFilename = new JWArrayList <string>(0); #endif //真正的开始预加载协成 StartCoroutine(PreloadCoroutine()); }
/// <summary> /// 获取预加载bundle列表 /// </summary> /// <returns></returns> public JWObjList <string> GetPreloadBundles() { JWObjList <string> bundleList = new JWObjList <string>(); ResPackConfig config = ResService.GetInstance().PackConfig; for (int i = 0; i < config.PackInfo.Count; i++) { BundlePackInfo p = config.PackInfo[i] as BundlePackInfo; if (p == null) { continue; } if (p.HasFlag(EBundleFlag.PreLoad)) { bundleList.Add(p.Path); } } return(bundleList); }
/// <summary> /// 获取已加载的bundle对应的资源 /// </summary> /// <param name="bundlePath"></param> /// <returns></returns> public JWObjList <string> GetLoadedBundleResources(string bundlePath) { if (string.IsNullOrEmpty(bundlePath) || _loadingResources.Count == 0) { return(null); } ResPackConfig config = ResService.GetInstance().PackConfig; if (config == null) { return(null); } BundlePackInfo pi = config.GetPackInfo(bundlePath) as BundlePackInfo; if (pi == null) { return(null); } for (int i = _loadingResources.Count - 1; i >= 0; i--) { string path = _loadingResources[i]; if (pi.Contains(path)) { if (!_relatedResources.Contains(path)) { _relatedResources.Add(path); } _loadingResources.Remove(path); } } return(_relatedResources); }
JWObjList <BundlePackInfo> GetBundleList(JWObjList <string> resources) { ResPackConfig config = ResService.GetInstance().PackConfig; if (config == null) { return(null); } JWObjList <BundlePackInfo> bundles = null; for (int i = 0; i < resources.Count; i++) { string path = resources[i]; if (ResService.GetInstance().Exists(path)) { continue; } BundlePackInfo bpi = config.GetPackInfoForResource(path) as BundlePackInfo; if (bpi != null) { if (bundles == null) { bundles = new JWObjList <BundlePackInfo>(); } if (!bundles.Contains(bpi)) { bundles.Add(bpi); } } } return(bundles); }
//进入游戏准备工作 private void DoReadyEnterGame() { #if USE_PACK_RES //加载主资源包配置 if (JW.Res.FileUtil.IsExistInIFSExtraFolder("MainResCfg.bytes")) { string resPackFile = JW.Res.FileUtil.CombinePath(JW.Res.FileUtil.GetIFSExtractPath(), "MainResCfg.bytes"); byte[] bbs = JW.Res.FileUtil.ReadFile(resPackFile); ResService.GetInstance().LoadResPackConfig(bbs); bbs = null; } //加载常驻AB AssetService.GetInstance().StartPreloadResidentBundle(delegate() { AssetService.GetInstance().StartPreloadAfterResident(OnAllPreloadCompelete); }); #else _stateArg.Progress = 0f; _stateArg.StateInfo = "准备进入游戏....."; EventService.GetInstance().SendEvent((uint)EventId.UpdateStateChange, _stateArg); //预加载基础资源 AssetService.GetInstance().StartPreloadAfterResident(OnAllPreloadCompelete); #endif }
/// <summary> /// 加载状态相关bundle /// </summary> /// <param name="resourceList">资源路径列表</param> /// <param name="complete">加载完成回调</param> /// <param name="progress">加载进度回调</param> /// <param name="loaded">单个bundle加载完成回调</param> /// <param name="singleSync">true:当只有一个bundle时,使用同步加载;否则使用并发异步加载</param> /// <returns></returns> public IEnumerator LoadBundle(JWObjList <string> resourceList, Action complete = null, BundleBatchLoadingDelegate progress = null, BundleLoadedOneDelegate loaded = null, bool singleSync = true) { bool record = loaded != null; // 剔除已有资源 for (int i = 0; i < resourceList.Count;) { string path = resourceList[i]; if (ResService.GetInstance().Exists(path)) { if (record) { _unbundledResources.Add(path); } resourceList.RemoveAt(i); continue; } i++; } // bundle list JWObjList <BundlePackInfo> bundleList = GetBundlePackInfoListForResources(resourceList, record); // 异步返回,在处理完源数据之后避免resourceList被reset yield return(null); // 未打包资源 if (_unbundledResources.Count > 0) { if (loaded != null) { loaded(_unbundledResources, true); } _unbundledResources.Clear(); } // load if (bundleList != null && bundleList.Count > 0) { if (singleSync && bundleList.Count == 1) { // 只有一个bundle,使用同步加载方式 BundleService.GetInstance().LoadSync(bundleList[0]); if (progress != null) { progress(1f); } if (loaded != null) { AssetBundle bundle = BundleService.GetInstance().GetBundle(bundleList[0].Path); JWObjList <string> relatedResources = GetLoadedBundleResources(bundleList[0].Path); if (relatedResources != null && relatedResources.Count > 0) { loaded(relatedResources, bundle != null); relatedResources.Clear(); } BundleService.GetInstance().Unload(bundleList[0]); } if (complete != null) { complete(); } } else { // 多个bundle,使用并发加载 yield return(StartCoroutine(BundleService.GetInstance().BatchLoadAsync(bundleList, delegate() { if (complete != null) { complete(); } }, progress, delegate(BundleRef bundle) { if (bundle != null) { if (loaded != null) { JWObjList <string> relatedResources = GetLoadedBundleResources(bundle.Path); if (relatedResources != null && relatedResources.Count > 0) { loaded(relatedResources, true); relatedResources.Clear(); } BundleService.GetInstance().Unload(bundle.PackInfo); } } }, delegate(BundlePackInfo pi, string error) { if (loaded != null) { JWObjList <string> relatedResources = GetLoadedBundleResources(pi.Path); if (relatedResources != null && relatedResources.Count > 0) { //bool succ = (pi.IsNoBundle()); //loaded(relatedResources, succ); //relatedResources.Clear(); } } }))); } } else { if (complete != null) { complete(); } } }
public static ResService GetResService() { return(ResService.GetInstance()); }
void OnGUI() { if (!BundleService.IsValidate()) { return; } JWObjDictionary <string, BundleRef> bundleDict = BundleService.GetInstance().BundleDict; GUILayout.Space(3f); GUILayout.BeginVertical(); // list title GUILayout.BeginHorizontal("Table Title", GUILayout.MinHeight(20f)); GUILayout.Label("Index", _labelStyle, GUILayout.Width(60f)); GUILayout.Label("RefCnt", _labelStyle, GUILayout.Width(60f)); GUILayout.Label("Tag", _labelStyle, GUILayout.Width(120f)); GUILayout.Label("Name", _labelStyle, GUILayout.Width(60f)); GUILayout.EndHorizontal(); ResPackConfig config = ResService.GetInstance().PackConfig; if (config == null) { return; } // list mScroll = GUILayout.BeginScrollView(mScroll); int index = 0; foreach (var kv in bundleDict) { BundleRef bundle = kv.Value; BundlePackInfo pi = config.GetPackInfo(bundle.Path) as BundlePackInfo; if (pi == null) { continue; } index++; GUILayout.BeginHorizontal("Table Row", GUILayout.MinHeight(20f)); // index GUILayout.Label(index.ToString(), _labelStyle, GUILayout.Width(60f)); // ref count GUILayout.Label(bundle.RefCnt.ToString(), _labelStyle, GUILayout.Width(60f)); // tag //GUILayout.Label(pi.Tag, _labelStyle, GUILayout.Width(120f)); // path GUILayout.Label(pi.Path, _labelStyle); GUILayout.EndHorizontal(); } GUILayout.EndScrollView(); GUILayout.EndVertical(); GUILayout.Space(3f); }