/// <summary> /// /// </summary> /// <param name="name"></param> void Load(string[] names) { IProgressResult <float, GameObject[]> result = resources.LoadAssetsAsync <GameObject>(names); result.Callbackable().OnProgressCallback(p => { Debug.LogFormat("Progress:{0}%", p * 100); }); result.Callbackable().OnCallback((r) => { try { if (r.Exception != null) { throw r.Exception; } foreach (GameObject template in r.Result) { GameObject.Instantiate(template); } } catch (Exception e) { Debug.LogErrorFormat("Load failure.Error:{0}", e); } }); }
public void LoadAsset <T>(string[] assetNames, Action <T[]> completed, Action <float> progress = null) where T : UnityEngine.Object { var resources = GetResources(); IProgressResult <float, T[]> result = resources.LoadAssetsAsync <T>(assetNames); result.Callbackable().OnProgressCallback(p => { if (progress != null) { progress(p * 100); } }); result.Callbackable().OnCallback((r) => { try { if (r.Exception != null) { throw r.Exception; } if (completed != null) { completed(r.Result); } } catch (Exception e) { Debug.LogErrorFormat("加载错误:{0}", e); } }); }
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)); } }
private void SetSubProgressCb(IProgressResult <float> progressResult) { progressResult.Callbackable().OnProgressCallback((progress => RaiseOnProgressCallback(0))); progressResult.Callbackable().OnCallback(progress => { finishProgress++; if (!CheckAllFinish()) { SetNextProgress(); } }); }
public virtual IProgressResult <float, Object[]> LoadAllAssetsAsync(string bundleName, System.Type type) { try { if (bundleName == null) { throw new System.ArgumentNullException("bundleName"); } if (type == null) { throw new System.ArgumentNullException("type"); } ProgressResult <float, Object[]> result = new ProgressResult <float, Object[]>(); IProgressResult <float, IBundle> bundleResult = this.LoadBundle(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, Object[]> assetResult = bundle.LoadAllAssetsAsync(type); 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); } }); } }); return(result); } catch (System.Exception e) { return(new ImmutableProgressResult <float, Object[]>(e, 0f)); } }
private void SetSubProgressCb(IProgressResult <TProgress> progressResult) { if (progressResult.IsDone) { return; } progressResult.Callbackable().OnProgressCallback((progress => RaiseOnProgressCallback(0))); progressResult.Callbackable().OnCallback(progress => { if (CheckAllFinish()) { RaiseFinish(); } }); }
public IEnumerator Download(List <string> bundleNames) { this.downloading = true; try { IProgressResult <Progress, BundleManifest> manifestResult = this.downloader.DownloadManifest(BundleSetting.ManifestFilename); yield return(manifestResult.WaitForDone()); if (manifestResult.Exception != null) { Debug.LogFormat("Downloads BundleManifest failure.Error:{0}", manifestResult.Exception); yield break; } BundleManifest manifest = manifestResult.Result; IProgressResult <float, List <BundleInfo> > bundlesResult = this.downloader.GetDownloadList(manifest); yield return(bundlesResult.WaitForDone()); List <BundleInfo> bundles = bundlesResult.Result.FindAll(obj => bundleNames.Contains(obj.FullName)); if (bundles == null || bundles.Count <= 0) { Debug.LogFormat("Please clear cache and remove StreamingAssets,try again."); yield break; } IProgressResult <Progress, bool> downloadResult = this.downloader.DownloadBundles(bundles); downloadResult.Callbackable().OnProgressCallback(p => { Debug.LogFormat("Downloading {0:F2}KB/{1:F2}KB {2:F3}KB/S", p.GetCompletedSize(UNIT.KB), p.GetTotalSize(UNIT.KB), p.GetSpeed(UNIT.KB)); }); yield return(downloadResult.WaitForDone()); if (downloadResult.Exception != null) { Debug.LogFormat("Downloads AssetBundle failure.Error:{0}", downloadResult.Exception); yield break; } if (this.resources != null) { //update BundleManager's manifest BundleManager manager = (this.resources as BundleResources).BundleManager as BundleManager; manager.BundleManifest = manifest; } #if UNITY_EDITOR UnityEditor.EditorUtility.OpenWithDefaultApp(BundleUtil.GetReadOnlyDirectory()); #endif } finally { this.downloading = false; } }
void LoadAsset(string name) { var resources = this.GetResources(); IProgressResult <float, GameObject> result = resources.LoadAssetAsync <GameObject>(name); result.Callbackable().OnCallback((r) => { try { if (r.Exception != null) { throw r.Exception; } GameObject.Instantiate(r.Result); } catch (Exception e) { Debug.LogErrorFormat("Load failure.Error:{0}", e); } }); }
public IEnumerator OnLoadAsset <T>(string[] assetNames, Action <T[]> completed, Action <float> progress = null) where T : UnityEngine.Object { var resources = GetResources(); IProgressResult <float, T[]> result = resources.LoadAssetsAsync <T>(assetNames); while (!result.IsDone) { if (progress != null) { progress(result.Progress * 100); } yield return(null); } result.Callbackable().OnCallback((r) => { try { if (r.Exception != null) { throw r.Exception; } if (completed != null) { completed(r.Result); } } catch (Exception e) { Debug.LogErrorFormat("加载错误:{0}", e); } }); yield return(null); }
protected virtual IEnumerator DoLoadBundle(IProgressPromise <float, IBundle[]> promise, BundleInfo[] bundleInfos, int priority) { List <IBundle> bundles = new List <IBundle>(); Exception exception = new Exception("unkown"); List <IProgressResult <float, IBundle> > bundleResults = new List <IProgressResult <float, IBundle> >(); for (int i = 0; i < bundleInfos.Length; i++) { try { DefaultBundle bundle = this.GetOrCreateBundle(bundleInfos[i], priority); IProgressResult <float, IBundle> bundleResult = bundle.Load(); bundleResult.Callbackable().OnCallback(r => { if (r.Exception != null) { exception = r.Exception; if (log.IsWarnEnabled) { log.WarnFormat("Loads Bundle failure! Error:{0}", r.Exception); } } else { bundles.Add(new InternalBundleWrapper((DefaultBundle)r.Result)); } }); if (!bundleResult.IsDone) { bundleResults.Add(bundleResult); } } catch (Exception e) { exception = e; if (log.IsWarnEnabled) { log.WarnFormat("Loads Bundle '{0}' failure! Error:{1}", bundleInfos[i], e); } } } bool finished = false; float progress = 0f; int count = bundleResults.Count; while (!finished && count > 0) { yield return(null); progress = 0f; finished = true; for (int i = 0; i < count; i++) { var result = bundleResults[i]; if (!result.IsDone) { finished = false; } progress += result.Progress; } promise.UpdateProgress(progress / count); } promise.UpdateProgress(1f); if (bundles.Count > 0) { promise.SetResult(bundles.ToArray()); } else { promise.SetException(exception); } }
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); } }
IEnumerator _CorDownload() { this.downloading = true; try { if (downloadProgressEvent != null) { downloadProgressEvent(0); } // 下载 Manifest IProgressResult <Progress, BundleManifest> manifestResult = this.downloader.DownloadManifest(BundleSetting.ManifestFilename); yield return(manifestResult.WaitForDone()); if (manifestResult.Exception != null) { LogManager.Log("Downloads BundleManifest failure.Error:{0}", manifestResult.Exception); yield break; } // 下载 BundleInfo BundleManifest manifest = manifestResult.Result; IProgressResult <float, List <BundleInfo> > bundlesResult = this.downloader.GetDownloadList(manifest); yield return(bundlesResult.WaitForDone()); List <BundleInfo> bundles = bundlesResult.Result; if (bundles == null || bundles.Count <= 0) { LogManager.Log("Please clear cache and remove StreamingAssets,try again."); yield break; } // 下载 Bundle IProgressResult <Progress, bool> downloadResult = this.downloader.DownloadBundles(bundles); downloadResult.Callbackable().OnProgressCallback(p => { LogManager.Log("Downloading {0:F2}KB/{1:F2}KB {2:F3}KB/S", p.GetCompletedSize(UNIT.KB), p.GetTotalSize(UNIT.KB), p.GetSpeed(UNIT.KB)); float percent = p.GetCompletedSize(UNIT.KB) / p.GetTotalSize(UNIT.KB); if (downloadProgressEvent != null) { downloadProgressEvent(percent); } }); yield return(downloadResult.WaitForDone()); if (downloadResult.Exception != null) { LogManager.Log("Downloads AssetBundle failure.Error:{0}", downloadResult.Exception); yield break; } // 下载成功 LogManager.Log(" 下载成功 "); IResources _resources = CreateResources(); context.GetContainer().Unregister <IResources>(); context.GetContainer().Register <IResources>(_resources); #if UNITY_EDITOR UnityEditor.EditorUtility.OpenWithDefaultApp(BundleUtil.GetStorableDirectory()); #endif } finally { this.downloading = false; } }
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); }
private void SetSubProgressCb(IProgressResult <float> progressResult) { progressResult.Callbackable().OnProgressCallback(f => RaiseOnProgressCallback(0)); progressResult.Callbackable().OnCallback(f => RaiseOnProgressCallback(0)); }