private IEnumerator _LoadCoroutine(string path, LoaderMode loaderMode, string package, LoadSceneMode loadSceneMode) { string scenePath = ResourceModuleConfig.GameResourcesDir + "/" + path; object getAsset = null; if (ResManager.IsEdiotrMode && Application.isEditor) { #if UNITY_EDITOR var allScenes = UnityEditor.EditorBuildSettings.scenes; UnityEditor.EditorBuildSettingsScene buildScene = null; foreach (var scene in allScenes) { if (scene.path == scenePath) { buildScene = scene; break; } } if (buildScene == null || !buildScene.enabled) { Debug.LogError("EditorMode must add scene to build settings and active:" + path); OnFinish(null); yield break; } ////编辑器状态下模拟异步加载延迟等待指定秒数 //if (loaderMode == LoaderMode.Async) //{ // yield return new WaitForSecondsRealtime(ResManager.EditorModeLoadDelay); //} #else Debug.LogErrorFormat("`IsEditorLoadAsset` is Unity Editor only"); #endif } else { DateTime beginTime = DateTime.Now; string bundlePath = string.IsNullOrEmpty(package) ? path : package; _bundleLoader = AssetBundleLoader.Load(bundlePath + ResourceModuleConfig.AssetBundleExt, null, loaderMode); while (!_bundleLoader.IsCompleted) { if (IsReadyDisposed) // 中途释放 { _bundleLoader.Release(); OnFinish(null); yield break; } this.Progress = _bundleLoader.Progress / 2f; yield return(null); } if (!_bundleLoader.IsSuccess || !_bundleLoader.Bundle.isStreamedSceneAssetBundle) { Debug.LogErrorFormat("[AssetFileLoader]Load BundleLoader Failed(Error) when Finished: {0}", path); _bundleLoader.Release(); OnFinish(null); yield break; } ResManager.LogLoadTime("AssetFileBridge", loaderMode, path, beginTime); } this.Progress = 0.5f; if (loaderMode == LoaderMode.Async) { var asyncOp = SceneManager.LoadSceneAsync(scenePath, loadSceneMode); while (!asyncOp.isDone) { this.Progress = 0.5f + asyncOp.progress / 2f; yield return(null); } } else { SceneManager.LoadScene(scenePath, loadSceneMode); } this.Progress = 1f; getAsset = true; OnFinish(getAsset); //加载完立即释放Loader ReleaseImmediate(); }
private IEnumerator _LoadCoroutine(string path, LoaderMode loaderMode, string package, Type type, bool loadAll) { if (type == null) { type = typeof(UnityEngine.Object); } object getAsset = null; if (ResManager.IsEdiotrMode && Application.isEditor) { #if UNITY_EDITOR string projPath = ResourceModuleConfig.BundleResoucesDir + "/" + path; if (!string.IsNullOrEmpty(package)) { string bundleName = UnityEditor.AssetDatabase.GetImplicitAssetBundleName(projPath); if (bundleName != package + ResourceModuleConfig.AssetBundleExt) { Debug.LogErrorFormat("Asset is BundleName set error: {0} {1}\n{2}", package, bundleName, projPath); } } if (loadAll) { getAsset = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(projPath); } else { getAsset = UnityEditor.AssetDatabase.LoadAssetAtPath(projPath, type); } if (getAsset == null) { Debug.LogErrorFormat("Asset is NULL(from {0} Folder): {1}", ResourceModuleConfig.BundleResoucesDir, path); } //编辑器状态下模拟异步加载延迟等待指定秒数 if (loaderMode == LoaderMode.Async) { yield return(new WaitForSecondsRealtime(ResManager.EditorModeLoadDelay)); } #else Debug.LogErrorFormat("`IsEditorLoadAsset` is Unity Editor only"); #endif } else { DateTime beginTime = DateTime.Now; string bundlePath = string.IsNullOrEmpty(package) ? path : package; _bundleLoader = AssetBundleLoader.Load(bundlePath + ResourceModuleConfig.AssetBundleExt, null, loaderMode); while (!_bundleLoader.IsCompleted) { if (IsReadyDisposed) // 中途释放 { _bundleLoader.Release(); OnFinish(null); yield break; } this.Progress = _bundleLoader.Progress / 2f; yield return(null); } if (!_bundleLoader.IsSuccess) { Debug.LogErrorFormat("[AssetFileLoader]Load BundleLoader Failed(Error) when Finished: {0}", path); _bundleLoader.Release(); OnFinish(null); yield break; } this.Progress = 0.5f; var assetBundle = _bundleLoader.Bundle; if (loadAll) { if (loaderMode == LoaderMode.Sync) { getAsset = assetBundle.LoadAllAssets(type); } else { var request = assetBundle.LoadAllAssetsAsync(type); while (!request.isDone) { this.Progress = 0.5f + request.progress / 2f; yield return(null); } getAsset = request.allAssets; } } else { var assetPath = ResourceModuleConfig.BundleResoucesDir + "/" + path; if (loaderMode == LoaderMode.Sync) { getAsset = assetBundle.LoadAsset(assetPath, type); } else { var request = assetBundle.LoadAssetAsync(assetPath, type); while (!request.isDone) { this.Progress = 0.5f + request.progress / 2f; yield return(null); } getAsset = request.asset; } } ResManager.LogLoadTime("AssetFileBridge", loaderMode, path, beginTime); if (getAsset == null) { Debug.LogErrorFormat("Asset is NULL: {0}", path); } } #if UNITY_EDITOR if (getAsset != null) { KResoourceLoadedAssetDebugger.Create(getAsset.GetType().Name, Url, getAsset); } #endif OnFinish(getAsset); }