コード例 #1
0
        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;
            }
        }
コード例 #2
0
ファイル: LevelDebug.cs プロジェクト: jxc608/SnaplingoScripts
    IEnumerator LoadConfigs()
    {
        var _resources = context.GetService <IResources> ();

        foreach (var item in configDic)
        {
            IProgressResult <float, TextAsset> result = _resources.LoadAssetAsync <TextAsset> (item.Value);
            while (!result.IsDone)
            {
                //LogManager.Log("Progress:{0}%", result.Progress * 100);
                yield return(null);
            }
            try
            {
                if (result.Exception != null)
                {
                    throw result.Exception;
                }
                item.Key.Fill(result.Result.text);
                LogManager.Log("Load : ", result.Result.name);
            }
            catch (Exception e)
            {
                LogManager.LogError("Load failure.Error:{0}", e);
            }
        }
        GameStart();
    }
コード例 #3
0
        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);
                }
            });
        }
コード例 #4
0
        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));
            }
        }
コード例 #5
0
        IEnumerator Start()
        {
            this.executor = new ThreadExecutor();

            IAsyncResult r1 = this.executor.Execute(Task1);

            yield return(r1.WaitForDone());

            IAsyncResult r2 = this.executor.Execute(promise => Task2(promise));

            yield return(r2.WaitForDone());

            IAsyncResult <string> r3 = this.executor.Execute <string> (promise => Task3(promise));

            yield return(new WaitForSeconds(0.5f));

            r3.Cancel();
            yield return(r3.WaitForDone());

            Debug.LogFormat("Task3 IsCalcelled:{0}", r3.IsCancelled);

            IProgressResult <float, string> r4 = this.executor.Execute <float, string> (promise => Task4(promise));

            while (!r4.IsDone)
            {
                yield return(null);

                Debug.LogFormat("Task4 Progress:{0}%", Mathf.FloorToInt(r4.Progress * 100));
            }

            Debug.LogFormat("Task4 Result:{0}", r4.Result);
        }
コード例 #6
0
        /// <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);
                }
            });
        }
コード例 #7
0
    IEnumerator _CopyManifestFromStreamingAssets(Action callback)
    {
        DirectoryInfo dir        = new DirectoryInfo(BundleUtil.GetReadOnlyDirectory() + "/");
        Uri           baseUri    = new Uri(dir.FullName);
        IDownloader   downloader = new WWWDownloader(baseUri, false);

        // 下载 Manifest
        IProgressResult <Progress, BundleManifest> manifestResult = downloader.DownloadManifest(BundleSetting.ManifestFilename);

        yield return(manifestResult.WaitForDone());

        if (manifestResult.Exception != null)
        {
            LogManager.Log("Downloads BundleManifest failure.Error:{0}", manifestResult.Exception);
            yield break;
        }

        IResources _resources = CreateResources();

        context.GetContainer().Register <IResources>(_resources);
        if (callback != null)
        {
            callback.Invoke();
        }
    }
コード例 #8
0
 /// <summary>
 /// 返回一个完成的IProgressResult<float>
 /// </summary>
 /// <returns></returns>
 public new static IProgressResult <TProgress> Void()
 {
     if (voidResult == null)
     {
         var result = new ProgressResult <TProgress>();
         result.SetResult();
         voidResult = result;
     }
     return(voidResult);
 }
コード例 #9
0
 public void AddAsyncResult(IProgressResult <TProgress> progressResult)
 {
     if (progressResult == null)
     {
         return;
     }
     _allProgress.Add(progressResult);
     //检查一下是否传入的任务全部都完成,在await的时候自动setResult
     alreadyDone = CheckAllFinish();
     SetSubProgressCb(progressResult);
 }
コード例 #10
0
 private void SetSubProgressCb(IProgressResult <float> progressResult)
 {
     progressResult.Callbackable().OnProgressCallback((progress => RaiseOnProgressCallback(0)));
     progressResult.Callbackable().OnCallback(progress =>
     {
         finishProgress++;
         if (!CheckAllFinish())
         {
             SetNextProgress();
         }
     });
 }
コード例 #11
0
 private void SetNextProgress()
 {
     if (progressQueue.Count > 0)
     {
         currentProgress = progressQueue.Dequeue().Invoke();
         SetSubProgressCb(currentProgress);
     }
     else
     {
         currentProgress = null;
     }
 }
コード例 #12
0
        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));
            }
        }
コード例 #13
0
 private void SetSubProgressCb(IProgressResult <TProgress> progressResult)
 {
     if (progressResult.IsDone)
     {
         return;
     }
     progressResult.Callbackable().OnProgressCallback((progress => RaiseOnProgressCallback(0)));
     progressResult.Callbackable().OnCallback(progress =>
     {
         if (CheckAllFinish())
         {
             RaiseFinish();
         }
     });
 }
コード例 #14
0
        /// <summary>
        /// Preloads AssetBundle.
        /// </summary>
        /// <param name="bundleNames"></param>
        /// <param name="priority"></param>
        /// <returns></returns>
        IEnumerator Preload(string[] bundleNames, int priority)
        {
            IProgressResult <float, IBundle[]> result = this.resources.LoadBundle(bundleNames, priority);

            yield return(result.WaitForDone());

            if (result.Exception != null)
            {
                Debug.LogWarningFormat("Loads failure.Error:{0}", result.Exception);
                yield break;
            }

            foreach (IBundle bundle in result.Result)
            {
                bundles.Add(bundle.Name, bundle);
            }
        }
コード例 #15
0
        public virtual IProgressResult <float, IBundle> Load()
        {
            if (this.result == null || this.result.Exception != null)
            {
                this.result = this.Execute <float, IBundle>(promise => Wrap(DoLoadBundleAndDependencies(promise)));
            }

            return(this.result);

            //ProgressResult<float, IBundle> resultCopy = new ProgressResult<float, IBundle>();
            //this.result.Callbackable().OnProgressCallback(p => resultCopy.UpdateProgress(p));
            //this.result.Callbackable().OnCallback((r) =>
            //{
            //    if (r.Exception != null)
            //        resultCopy.SetException(r.Exception);
            //    else
            //        resultCopy.SetResult(new InternalBundleWrapper(this));
            //});
            //return resultCopy;
        }
コード例 #16
0
        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);
        }
コード例 #17
0
        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);
                }
            });
        }
コード例 #18
0
    //void LoadBundleCallback()
    //{

    //}

    IEnumerator LoadConfigs()
    {
        var _resources = context.GetService <IResources>();
        int i          = 0;

        foreach (var item in configDic)
        {
            IProgressResult <float, TextAsset> result = _resources.LoadAssetAsync <TextAsset>(item.Value);
            while (!result.IsDone)
            {
                //LogManager.Log("Progress:{0}%", result.Progress * 100);
                yield return(null);
            }
            try
            {
                ++i;
                m_fConfigProgress = (float)((float)i / (float)configDic.Count);
                if (result.Exception != null)
                {
                    throw result.Exception;
                }
                item.Key.Fill(result.Result.text);
                LogManager.Log(result.Result.name);
            }
            catch (Exception e)
            {
                LogManager.LogError("Load failure.Error:{0}", e);
            }
        }
        yield return(null);

        if (null != m_onConfigLoadCallback)
        {
            m_onConfigLoadCallback.Invoke();
            m_onConfigLoadCallback = null;
        }
        //GameStart();
    }
コード例 #19
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        IEnumerator Load2(string name)
        {
            IProgressResult <float, GameObject> result = resources.LoadAssetAsync <GameObject>(name);

            while (!result.IsDone)
            {
                Debug.LogFormat("Progress:{0}%", result.Progress * 100);
                yield return(null);
            }

            try
            {
                if (result.Exception != null)
                {
                    throw result.Exception;
                }

                GameObject.Instantiate(result.Result);
            }
            catch (Exception e)
            {
                Debug.LogErrorFormat("Load failure.Error:{0}", e);
            }
        }
コード例 #20
0
ファイル: MulProgressResult.cs プロジェクト: Nutarb/Framework
 public void AddProgress(IProgressResult <float> progressResult)
 {
     _allProgress.Add(progressResult);
     SetSubProgressCb(progressResult);
 }
コード例 #21
0
ファイル: MulProgressResult.cs プロジェクト: Nutarb/Framework
 private void SetSubProgressCb(IProgressResult <float> progressResult)
 {
     progressResult.Callbackable().OnProgressCallback(f => RaiseOnProgressCallback(0));
     progressResult.Callbackable().OnCallback(f => RaiseOnProgressCallback(0));
 }
コード例 #22
0
        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);
        }
コード例 #23
0
    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;
        }
    }
コード例 #24
0
 public ProgressCallbackable(IProgressResult <TProgress> result)
 {
     this._result = result;
 }
コード例 #25
0
        protected virtual IEnumerator DoLoadBundleAndDependencies(IProgressPromise <float, IBundle> promise)
        {
            this.startTime = Time.realtimeSinceStartup;
            List <IProgressResult <float, AssetBundle> > results = new List <IProgressResult <float, AssetBundle> >();

            BundleLoader currLoader = manager.GetOrCreateBundleLoader(this.BundleInfo, this.Priority);

            currLoader.Retain();
            loaders.Add(currLoader);

            IProgressResult <float, AssetBundle> currResult = currLoader.LoadAssetBundle();

            if (!currResult.IsDone)
            {
                results.Add(currResult);
            }

            var dependencies = manager.GetOrCreateDependencies(this.BundleInfo, true, this.Priority);

            for (int i = 0; i < dependencies.Count; i++)
            {
                var dependency = dependencies[i];
                dependency.Retain();
                this.loaders.Add(dependency);

                var result = dependency.LoadAssetBundle();
                if (!result.IsDone)
                {
                    results.Add(result);
                }
            }

            bool  finished     = false;
            float progress     = 0f;
            float timeProgress = 0f;
            int   count        = results.Count;

            while (!finished && count > 0)
            {
                yield return(null);

                progress = 0f;
                finished = true;
                for (int i = 0; i < count; i++)
                {
                    var result = results[i];
                    if (!result.IsDone)
                    {
                        finished = false;
                    }

                    progress += result.Progress;
                }

                timeProgress = TIME_PROGRESS_WEIGHT * Mathf.Atan(Time.realtimeSinceStartup - this.startTime) * 2 / Mathf.PI;
                promise.UpdateProgress(timeProgress + (1.0f - TIME_PROGRESS_WEIGHT) * progress / count);
            }

            promise.UpdateProgress(1f);

            yield return(null);

            if (currResult.Exception != null)
            {
                promise.SetException(currResult.Exception);
            }
            else
            {
                this.assetBundle = currResult.Result;
                promise.SetResult(this);
            }
        }
コード例 #26
0
 public SequenceProgress(IProgressResult <float> progress, params Func <IProgressResult <float> >[] allProgress) : this(false, allProgress)
 {
     currentProgress = progress;
     SetSubProgressCb(currentProgress);
     AddAsyncResult(allProgress);
 }
コード例 #27
0
        protected virtual IEnumerator DoLoadBundleAndDependencies(IProgressPromise <float, IBundle> promise)
        {
            this.startTime = Time.realtimeSinceStartup;
            List <IProgressResult <float, AssetBundle> > results    = new List <IProgressResult <float, AssetBundle> >();
            IProgressResult <float, AssetBundle>         currResult = this.LoadAssetBundle();

            if (!currResult.IsDone)
            {
                results.Add(currResult);
            }

            var all = this.GetDependencies(true);

            for (int i = 0; i < all.Count; i++)
            {
                var result = all[i].LoadAssetBundle();
                if (!result.IsDone)
                {
                    results.Add(result);
                }
            }

            bool  finished     = false;
            float progress     = 0f;
            float timeProgress = 0f;
            int   count        = results.Count;

            while (!finished && count > 0)
            {
                yield return(null);

                progress = 0f;
                finished = true;
                for (int i = 0; i < count; i++)
                {
                    var result = results[i];
                    if (!result.IsDone)
                    {
                        finished = false;
                    }

                    progress += result.Progress;
                }

                timeProgress = TIME_PROGRESS_WEIGHT * Mathf.Atan(Time.realtimeSinceStartup - this.startTime) * 2 / Mathf.PI;
                promise.UpdateProgress(timeProgress + (1.0f - TIME_PROGRESS_WEIGHT) * progress / count);
            }

            promise.UpdateProgress(1f);

            yield return(null);

            if (currResult.Exception != null)
            {
                promise.SetException(currResult.Exception);
            }
            else
            {
                promise.SetResult(this);
            }
        }
コード例 #28
0
        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);
            }
        }
コード例 #29
0
        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);
            }
        }