示例#1
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));
            }
        }
示例#2
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));
            }
        }
示例#3
0
        public virtual IProgressResult <float, IBundle> LoadBundle(BundleInfo bundleInfo, int priority)
        {
            try
            {
                if (bundleInfo == null)
                {
                    throw new ArgumentNullException("The bundleInfo is null!");
                }

                DefaultBundle bundle = this.GetOrCreateBundle(bundleInfo, priority);
                var           result = bundle.Load();

                ProgressResult <float, IBundle> resultCopy = new ProgressResult <float, IBundle>();
                result.Callbackable().OnProgressCallback(p => resultCopy.UpdateProgress(p));
                result.Callbackable().OnCallback((r) =>
                {
                    if (r.Exception != null)
                    {
                        resultCopy.SetException(r.Exception);
                    }
                    else
                    {
                        resultCopy.SetResult(new InternalBundleWrapper(bundle));
                    }
                });
                return(resultCopy);
            }
            catch (Exception e)
            {
                return(new ImmutableProgressResult <float, IBundle>(e, 0f));
            }
        }
示例#4
0
        private void InternalOpen <T>(Type type, ProgressResult <float, T> promise, ViewModel viewModel) where T : View
        {
            View view = null;
            var  path = (GetClassData(type).Attribute as UIAttribute).Path;

            promise.Callbackable().OnCallback(progressResult =>
            {
                Sort(view);
                view.Show();
            });
            if (openedSingleViews.TryGetValue(type, out var view1))
            {
                promise.UpdateProgress(1);
                promise.SetResult(view1);
            }
            else
            {
                view = ReflectionHelper.CreateInstance(type) as View;
                Executors.RunOnCoroutineNoReturn(CreateViewGo(promise, view, path, viewModel));
            }
            openedViews.Add(view);
            if (view.IsSingle)
            {
                openedSingleViews[type] = view;
            }
        }
示例#5
0
        /// <summary>
        /// 等待多久
        /// </summary>
        /// <param name="duration">单位s</param>
        /// <returns></returns>
        public static IProgressResult <float> WaitAsync(float duration)
        {
            ProgressResult <float> result = new ProgressResult <float>(true);
            Timer timer = new Timer();

            timer.Init(duration, onComplete: () => result.SetResult(), onUpdate: val =>
            {
                result.UpdateProgress(val / duration);
            }, false, false, null);
            Timer._manager.RegisterTimer(timer);
            return(result);
        }
示例#6
0
 private void InternalOpen <T>(Type type, ProgressResult <float, T> promise, ViewModel viewModel) where T : View
 {
     promise.Callbackable().OnCallback(progressResult =>
     {
         var _view = progressResult.Result;
         Sort(_view);
         _view.Show();
         if (_view.IsSingle)
         {
             _openedViews[type] = _view;
         }
     });
     if (_openedViews.TryGetValue(type, out var view))
     {
         promise.UpdateProgress(1);
         promise.SetResult(view);
     }
     else
     {
         Executors.RunOnCoroutineNoReturn(CreateView(promise, type, viewModel));
     }
 }
示例#7
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)));
            }

            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, r.Result));
                }
            });
            return(resultCopy);
        }