/// <summary> /// 强制回收所有资源 /// </summary> public static void ForceReleaseAll() { for (int i = 0; i < _fileLoaders.Count; i++) { AssetFileLoader loader = _fileLoaders[i]; loader.UnLoad(true); } _fileLoaders.Clear(); // 释放所有资源 Resources.UnloadUnusedAssets(); }
/// <summary> /// 资源回收 /// 卸载引用计数为零的资源 /// </summary> public static void Release() { for (int i = _fileLoaders.Count - 1; i >= 0; i--) { AssetFileLoader loader = _fileLoaders[i]; if (loader.IsDone() && loader.RefCount <= 0) { loader.UnLoad(true); _fileLoaders.RemoveAt(i); } } }
/// <summary> /// 从缓存列表里获取加载器,如果不存在创建一个新的加载器并添加到列表 /// </summary> public static AssetFileLoader GetFileLoader(bool isStreamScene, string loadPath, System.Action <AssetFileLoader> callback, string manifestPath) { // 如果已经提交相同请求 AssetFileLoader loader = TryGetFileLoaderInternal(loadPath); if (loader != null) { loader.Reference(); //引用计数 if (loader.IsDone()) { if (callback != null) { callback.Invoke(loader); } } else { if (callback != null) { loader.LoadCallback += callback; } } return(loader); } // 创建加载器 AssetFileLoader newLoader = null; if (AssetLoadMode == EAssetLoadMode.EditorMode) { newLoader = new AssetDatabaseLoader(isStreamScene, loadPath); } else if (AssetLoadMode == EAssetLoadMode.ResourceMode) { newLoader = new AssetResourceLoader(isStreamScene, loadPath); } else if (AssetLoadMode == EAssetLoadMode.BundleMode) { newLoader = new AssetBundleLoader(isStreamScene, loadPath, manifestPath); } else { throw new NotImplementedException($"{AssetLoadMode}"); } // 新增下载需求 _fileLoaders.Add(newLoader); newLoader.LoadCallback = callback; newLoader.Reference(); //引用计数 newLoader.Update(); //立刻轮询 return(newLoader); }
public static int GetFileLoaderFailedCount() { int count = 0; for (int i = 0; i < _fileLoaders.Count; i++) { AssetFileLoader temp = _fileLoaders[i]; if (temp.LoadState == EAssetFileLoadState.LoadAssetFileFailed) { count++; } } return(count); }
private static AssetFileLoader TryGetFileLoaderInternal(string assetPath) { AssetFileLoader loader = null; for (int i = 0; i < _fileLoaders.Count; i++) { AssetFileLoader temp = _fileLoaders[i]; if (temp.LoadPath.Equals(assetPath)) { loader = temp; break; } } return(loader); }
public override void Update() { // 轮询更新所有对象加载器 if (LoadState == EAssetFileLoadState.LoadAssetFileOK) { UpdateAllAssetObjectLoader(); } if (IsDone()) { return; } if (LoadState == EAssetFileLoadState.None) { LoadState = EAssetFileLoadState.LoadDepends; } // 1. 加载所有依赖项 if (LoadState == EAssetFileLoadState.LoadDepends) { string[] dependencies = AssetSystem.BundleMethod.GetDirectDependencies(_manifestPath); if (dependencies.Length > 0) { foreach (string dpManifestPath in dependencies) { string dpLoadPath = AssetSystem.BundleMethod.GetAssetBundleLoadPath(dpManifestPath); AssetFileLoader dpLoader = AssetSystem.GetFileLoader(false, dpLoadPath, null, dpManifestPath); _depends.Add(dpLoader); } } LoadState = EAssetFileLoadState.CheckDepends; } // 2. 检测所有依赖完成状态 if (LoadState == EAssetFileLoadState.CheckDepends) { foreach (var dpLoader in _depends) { if (dpLoader.IsDone() == false) { return; } } LoadState = EAssetFileLoadState.LoadAssetFile; } // 3. 加载AssetBundle if (LoadState == EAssetFileLoadState.LoadAssetFile) { #if UNITY_EDITOR // 注意:Unity2017.4编辑器模式下,如果AssetBundle文件不存在会导致编辑器崩溃,这里做了预判。 if (System.IO.File.Exists(LoadPath) == false) { LogSystem.Log(ELogType.Warning, $"Not found assetBundle file : {LoadPath}"); LoadState = EAssetFileLoadState.LoadAssetFileFailed; LoadCallback?.Invoke(this); return; } #endif // Load assetBundle file _cacheRequest = AssetBundle.LoadFromFileAsync(LoadPath); LoadState = EAssetFileLoadState.CheckAssetFile; } // 4. 检测AssetBundle加载结果 if (LoadState == EAssetFileLoadState.CheckAssetFile) { if (_cacheRequest.isDone == false) { return; } _cacheBundle = _cacheRequest.assetBundle; // Check scene if (IsStreamScene) { LoadState = EAssetFileLoadState.LoadAssetFileOK; LoadCallback?.Invoke(this); return; } // Check error if (_cacheBundle == null) { LogSystem.Log(ELogType.Warning, $"Failed to load assetBundle file : {LoadPath}"); LoadState = EAssetFileLoadState.LoadAssetFileFailed; LoadCallback?.Invoke(this); } else { LoadState = EAssetFileLoadState.LoadAssetFileOK; LoadCallback?.Invoke(this); } } }