public virtual IProgressResult <float, T> LoadAssetAsync <T>(string path) where T : Object { try { if (string.IsNullOrEmpty(path)) { throw new System.ArgumentNullException("path", "The path is null or empty!"); } ProgressResult <float, T> result = new ProgressResult <float, T>(); AssetPathInfo pathInfo = this.pathInfoParser.Parse(path); if (pathInfo == null) { throw new System.Exception(string.Format("Not found the AssetBundle or parses the path info '{0}' failure.", path)); } T asset = this.GetCache <T>(path); if (asset != null) { result.UpdateProgress(1f); result.SetResult(asset); return(result); } IProgressResult <float, IBundle> bundleResult = this.LoadBundle(pathInfo.BundleName); float weight = bundleResult.IsDone ? 0f : DEFAULT_WEIGHT; bundleResult.Callbackable().OnProgressCallback(p => result.UpdateProgress(p * weight)); bundleResult.Callbackable().OnCallback((r) => { if (r.Exception != null) { result.SetException(r.Exception); return; } using (IBundle bundle = r.Result) { IProgressResult <float, T> assetResult = bundle.LoadAssetAsync <T>(pathInfo.AssetName); assetResult.Callbackable().OnProgressCallback(p => result.UpdateProgress(weight + (1f - weight) * p)); assetResult.Callbackable().OnCallback((ar) => { if (ar.Exception != null) { result.SetException(ar.Exception); } else { result.SetResult(ar.Result); this.AddCache <T>(path, ar.Result); } }); } }); return(result); } catch (System.Exception e) { return(new ImmutableProgressResult <float, T>(e, 0f)); } }
protected override IEnumerator DoLoadSceneAsync(ISceneLoadingPromise <Scene> promise, string path, LoadSceneMode mode = LoadSceneMode.Single) { AssetPathInfo pathInfo = pathInfoParser.Parse(path); if (pathInfo == null) { promise.Progress = 0f; promise.SetException(string.Format("Parses the path info '{0}' failure.", path)); yield break; } var name = string.Format("{0}{1}", ASSETS, pathInfo.AssetName); #if UNITY_2018_3_OR_NEWER AsyncOperation operation = EditorSceneManager.LoadSceneAsyncInPlayMode(name, new LoadSceneParameters(mode)); #else AsyncOperation operation = LoadSceneMode.Additive.Equals(mode) ? EditorApplication.LoadLevelAdditiveAsyncInPlayMode(name) : EditorApplication.LoadLevelAsyncInPlayMode(name); #endif if (operation == null) { promise.SetException(string.Format("Not found the scene '{0}'.", path)); yield break; } operation.allowSceneActivation = false; while (operation.progress < 0.9f) { if (operation.progress == 0f) { operation.priority = promise.Priority; } promise.Progress = operation.progress; yield return(waitForSeconds); } promise.Progress = operation.progress; promise.State = LoadState.SceneActivationReady; while (!operation.isDone) { if (promise.AllowSceneActivation && !operation.allowSceneActivation) { operation.allowSceneActivation = promise.AllowSceneActivation; } promise.Progress = operation.progress; yield return(waitForSeconds); } Scene scene = SceneManager.GetSceneByName(Path.GetFileNameWithoutExtension(pathInfo.AssetName)); if (!scene.IsValid()) { promise.SetException(string.Format("Not found the scene '{0}'.", path)); yield break; } promise.Progress = 1f; promise.SetResult(scene); }
protected override IEnumerator DoLoadSceneAsync(ISceneLoadingPromise <Scene> promise, string path, LoadSceneMode mode = LoadSceneMode.Single) { AssetPathInfo pathInfo = pathInfoParser.Parse(path); if (pathInfo == null) { promise.Progress = 0f; promise.SetException(string.Format("Parses the path info '{0}' failure.", path)); yield break; } AsyncOperation operation = SceneManager.LoadSceneAsync(Path.GetFileNameWithoutExtension(pathInfo.AssetName), mode); if (operation == null) { promise.SetException(string.Format("Not found the scene '{0}'.", path)); yield break; } operation.allowSceneActivation = false; while (operation.progress < 0.9f) { if (operation.progress == 0f) { operation.priority = promise.Priority; } promise.Progress = operation.progress; yield return(waitForSeconds); } promise.Progress = operation.progress; promise.State = LoadState.SceneActivationReady; while (!operation.isDone) { if (promise.AllowSceneActivation && !operation.allowSceneActivation) { operation.allowSceneActivation = promise.AllowSceneActivation; } promise.Progress = operation.progress; yield return(waitForSeconds); } Scene scene = SceneManager.GetSceneByName(Path.GetFileNameWithoutExtension(pathInfo.AssetName)); if (!scene.IsValid()) { promise.SetException(string.Format("Not found the scene '{0}'.", path)); yield break; } promise.Progress = 1f; promise.SetResult(scene); }
public virtual Object LoadAsset(string path, System.Type type) { if (string.IsNullOrEmpty(path)) { throw new System.ArgumentNullException("path", "The path is null or empty!"); } if (type == null) { throw new System.ArgumentNullException("type"); } AssetPathInfo pathInfo = this.pathInfoParser.Parse(path); if (pathInfo == null) { throw new System.Exception(string.Format("Not found the AssetBundle or parses the path info '{0}' failure.", path)); } Object asset = this.GetCache <Object>(path); if (asset != null) { return(asset); } using (IBundle bundle = this.GetBundle(pathInfo.BundleName)) { if (bundle == null) { if (log.IsWarnEnabled) { log.WarnFormat("The AssetBundle of the current asset(path:{0}) is not loaded, please load the AssetBundle first.", path); } return(null); } asset = bundle.LoadAsset(pathInfo.AssetName, type); if (asset != null) { this.AddCache(path, asset); } return(asset); } }
protected override IEnumerator DoLoadSceneAsync(ISceneLoadingPromise <Scene> promise, string path, LoadSceneMode mode = LoadSceneMode.Single) { AssetPathInfo pathInfo = pathInfoParser.Parse(path); if (pathInfo == null) { promise.Progress = 1f; promise.SetException(string.Format("Parses the path info '{0}' failure.", path)); yield break; } yield return(null);//Wait for a frame. IProgressResult <float, IBundle> bundleResult = this.LoadBundle(pathInfo.BundleName, promise.Priority); float weight = bundleResult.IsDone ? 0f : DEFAULT_WEIGHT; bundleResult.Callbackable().OnProgressCallback(p => promise.Progress = p * weight); while (!bundleResult.IsDone) { yield return(null); } if (bundleResult.Exception != null) { promise.SetException(bundleResult.Exception); yield break; } promise.State = LoadState.AssetBundleLoaded; using (IBundle bundle = bundleResult.Result) { AsyncOperation operation = SceneManager.LoadSceneAsync(Path.GetFileNameWithoutExtension(pathInfo.AssetName), mode); if (operation == null) { promise.SetException(string.Format("Not found the scene '{0}'.", path)); yield break; } operation.priority = promise.Priority; operation.allowSceneActivation = false; while (operation.progress < 0.9f) { promise.Progress = weight + (1f - weight) * operation.progress; yield return(waitForSeconds); } promise.Progress = weight + (1f - weight) * operation.progress; promise.State = LoadState.SceneActivationReady; while (!operation.isDone) { if (promise.AllowSceneActivation && !operation.allowSceneActivation) { operation.allowSceneActivation = promise.AllowSceneActivation; } promise.Progress = weight + (1f - weight) * operation.progress; yield return(waitForSeconds); } Scene scene = SceneManager.GetSceneByName(Path.GetFileNameWithoutExtension(pathInfo.AssetName)); if (!scene.IsValid()) { promise.SetException(string.Format("Not found the scene '{0}'.", path)); yield break; } promise.Progress = 1f; promise.SetResult(scene); } }
protected virtual IEnumerator DoLoadAssetsToMapAsync <T>(IProgressPromise <float, Dictionary <string, T> > promise, params string[] paths) where T : Object { Dictionary <string, T> results = new Dictionary <string, T>(); Dictionary <string, List <string> > groups = new Dictionary <string, List <string> >(); Dictionary <string, string> assetNameAndPathMapping = new Dictionary <string, string>(); List <string> bundleNames = new List <string>(); for (int i = 0; i < paths.Length; i++) { var path = paths[i]; AssetPathInfo pathInfo = this.pathInfoParser.Parse(path); if (pathInfo == null || pathInfo.BundleName == null) { if (log.IsWarnEnabled) { log.WarnFormat("Not found the AssetBundle or parses the path info '{0}' failure.", path); } continue; } var asset = this.GetCache <T>(path); if (asset != null) { if (!results.ContainsKey(path)) { results.Add(path, asset); } continue; } List <string> list = null; if (!groups.TryGetValue(pathInfo.BundleName, out list)) { list = new List <string>(); groups.Add(pathInfo.BundleName, list); bundleNames.Add(pathInfo.BundleName); } if (!list.Contains(pathInfo.AssetName)) { list.Add(pathInfo.AssetName); assetNameAndPathMapping[pathInfo.AssetName] = path; } } if (bundleNames.Count <= 0) { promise.UpdateProgress(1f); promise.SetResult(results); yield break; } IProgressResult <float, IBundle[]> bundleResult = this.LoadBundle(bundleNames.ToArray(), 0); float weight = bundleResult.IsDone ? 0f : DEFAULT_WEIGHT; bundleResult.Callbackable().OnProgressCallback(p => promise.UpdateProgress(weight * p)); yield return(bundleResult.WaitForDone()); if (bundleResult.Exception != null) { promise.SetException(bundleResult.Exception); yield break; } Dictionary <string, IProgressResult <float, Dictionary <string, T> > > assetResults = new Dictionary <string, IProgressResult <float, Dictionary <string, T> > >(); IBundle[] bundles = bundleResult.Result; for (int i = 0; i < bundles.Length; i++) { using (IBundle bundle = bundles[i]) { if (!groups.ContainsKey(bundle.Name)) { continue; } List <string> assetNames = groups[bundle.Name]; if (assetNames == null || assetNames.Count < 0) { continue; } IProgressResult <float, Dictionary <string, T> > assetResult = bundle.LoadAssetsToMapAsync <T>(assetNames.ToArray()); assetResult.Callbackable().OnCallback(ar => { if (ar.Exception != null) { return; } foreach (var kv in ar.Result) { string key = assetNameAndPathMapping[kv.Key]; var value = kv.Value; if (!results.ContainsKey(key)) { results.Add(key, value); } } }); assetResults.Add(bundle.Name, assetResult); } } if (assetResults.Count < 0) { promise.UpdateProgress(1f); promise.SetResult(results); yield break; } bool finished = false; float progress = 0f; int assetCount = assetResults.Count; do { yield return(waitForSeconds); finished = true; progress = 0f; var assetEnumerator = assetResults.GetEnumerator(); while (assetEnumerator.MoveNext()) { var kv = assetEnumerator.Current; var assetResult = kv.Value; if (!assetResult.IsDone) { finished = false; } progress += (1f - weight) * assetResult.Progress / assetCount; } promise.UpdateProgress(weight + progress); } while (!finished); promise.UpdateProgress(1f); promise.SetResult(results); }