Exemple #1
0
        private IEnumerator RequestCoroutine(AsyncTaskInternal result, System.Action <Http> onSuccess, System.Action <Exception> onFail)
        {
            AsyncOperation op = webRequest.Send();

            while (!op.isDone)
            {
                if (result.IsAbort)
                {
                    webRequest.Abort();
                }
                result.SetProgress(op.progress);
                yield return(null);
            }
            result.Done();
            if (webRequest.isError)
            {
                if (!string.IsNullOrEmpty(webRequest.error))
                {
                    if (onFail != null)
                    {
                        onFail(new UnityException(webRequest.error));
                    }
                }
            }
            else
            {
                if (onSuccess != null)
                {
                    onSuccess(this);
                }
            }
        }
Exemple #2
0
        public AsyncTask LoadAsset <T> (string assetBundleName, string assetName, System.Action <T> onSuccess = null, System.Action <System.Exception> onFail = null) where T : UnityEngine.Object
        {
                        #if UNITY_EDITOR
            if (IsSimulate)
            {
                string[] assetPaths = UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(assetBundleName, assetName);
                if (assetPaths.Length == 0)
                {
                    if (onFail != null)
                    {
                        onFail(new Exception("There is no asset with name \"" + assetName + "\" in " + assetBundleName));
                    }
                    return(AsyncTaskInternal.Complete());
                }
                T target = UnityEditor.AssetDatabase.LoadMainAssetAtPath(assetPaths [0]) as T;
                if (onSuccess != null)
                {
                    onSuccess(target);
                }
                return(AsyncTaskInternal.Complete());
            }
                        #endif

            AsyncTaskInternal result = new AsyncTaskInternal(assetName);
            var task = LoadAssetBundle(assetBundleName, (bundle) => {
                result.SetProgress(0.5f);
                var innerTask = this.StartCoroutineEx(LoadAssetCoroutine <T> (bundle, assetName, (asset) => {
                    result.Done();
                    if (onSuccess != null)
                    {
                        onSuccess(asset);
                    }
                }, (ex) => {
                    result.Done();
                    if (onFail != null)
                    {
                        onFail(ex);
                    }
                }));
                result.SetOnAbort(
                    () => {
                    innerTask.Abort();
                    if (onFail != null)
                    {
                        onFail(new Exception("Aborted"));
                    }
                }
                    );
            }, (ex) => {
                result.Done();
                if (onFail != null)
                {
                    onFail(ex);
                }
            });
            result.SetOnAbort(() => {
                task.Abort();
            });
            return(result);
        }
Exemple #3
0
        public AsyncTask LoadAssetBundleManifest(string baseUri, System.Action <AssetBundleManifest> onSuccess = null, System.Action <System.Exception> onFail = null)
        {
                        #if UNITY_EDITOR
            if (IsSimulate)
            {
                return(AsyncTaskInternal.Complete());
            }
                        #endif


            this.baseUri = new Uri(baseUri);
            string            manifestAssetName = new System.IO.DirectoryInfo(this.baseUri.AbsolutePath).Name;
            AsyncTaskInternal result            = new AsyncTaskInternal(manifestAssetName);

            var task = LoadAssetBundleInternal(manifestAssetName, false, (bundle) => {
                result.SetProgress(0.5f);
                var innerTask = this.StartCoroutineEx(LoadAssetCoroutine <AssetBundleManifest> (bundle, "AssetBundleManifest",
                                                                                                (manifest) => {
                    result.Done();
                    this.manifest = manifest;
                    if (onSuccess != null)
                    {
                        onSuccess(manifest);
                    }
                },
                                                                                                (ex) => {
                    result.Done();
                    if (onFail != null)
                    {
                        onFail(ex);
                    }
                }));
                result.SetOnAbort(() => {
                    innerTask.Abort();
                    if (onFail != null)
                    {
                        onFail(new Exception("Aborted"));
                    }
                });
            }
                                               , (ex) => {
                result.Done();
                if (onFail != null)
                {
                    onFail(ex);
                }
            });

            result.SetOnAbort(() => {
                task.Abort();
            });
            return(result);
        }
Exemple #4
0
        public AsyncTask LoadAssetBundle(string assetBundleName, System.Action <UnityEngine.AssetBundle> onSuccess = null, System.Action <System.Exception> onFail = null)
        {
            assetBundleName = RemapVariantName(assetBundleName);
            string[] dependencies = null;
            if (!this.cachedDependencies.TryGetValue(assetBundleName, out dependencies))
            {
                dependencies = CachingDependencies(assetBundleName);
                this.cachedDependencies.Add(assetBundleName, dependencies);
            }
            AsyncTaskInternal result = new AsyncTaskInternal("LoadAssetBundle " + assetBundleName);

            var task = this.StartCoroutineEx(LoadAssetBundleAll(dependencies,
                                                                () => {
                result.SetProgress(0.5f);
                var innerTask = LoadAssetBundleInternal(assetBundleName, false,
                                                        (bundle) => {
                    result.Done();
                    if (onSuccess != null)
                    {
                        onSuccess(bundle);
                    }
                },
                                                        (ex) => {
                    result.Done();
                    if (onFail != null)
                    {
                        onFail(ex);
                    }
                });
                result.SetOnAbort(() => {
                    innerTask.Abort();
                });
            },
                                                                (ex) => {
                result.Done();
                if (onFail != null)
                {
                    onFail(ex);
                }
            }));

            result.SetOnAbort(
                () => {
                task.Abort();
                if (onFail != null)
                {
                    onFail(new Exception("Aborted"));
                }
            }
                );

            return(result);
        }