示例#1
0
        public static AsyncTask StartCoroutineEx(this MonoBehaviour self, IEnumerator iEnumerator)
        {
            AsyncTaskInternal t = new AsyncTaskInternal("coroutine");

            self.StartCoroutine(GenericRoutine(t, iEnumerator));
            return(t);
        }
示例#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);
        }
示例#3
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);
                }
            }
        }
示例#4
0
        public AsyncTask Request(MonoBehaviour behaviour)
        {
            AsyncTaskInternal asyncResult = new AsyncTaskInternal(webRequest.url);

            behaviour.StartCoroutine(RequestCoroutine(asyncResult, onSuccess, onFail));

            return(asyncResult);
        }
 public AsyncTask WaitForValueCreated(MonoBehaviour behaviour, Action <T> onSuccess, Action <Exception> onFail)
 {
     if (State == CachedState.NotCached)
     {
         onFail(new Exception("Cache is not started yet"));
         return(AsyncTaskInternal.Complete());
     }
     return(behaviour.StartCoroutineEx(WaitForValueCreated(onSuccess, onFail)));
 }
示例#6
0
 private static IEnumerator GenericRoutine(AsyncTaskInternal t, IEnumerator iEnumerator)
 {
     while (iEnumerator.MoveNext())
     {
         if (t.IsAbort)
         {
             yield break;
         }
         yield return(null);
     }
     t.Done();
 }
示例#7
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);
        }
示例#8
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);
        }
示例#9
0
        private AsyncTask LoadAssetBundleInternal(string assetBundleName, bool isManifest, System.Action <UnityEngine.AssetBundle> onSuccess = null, System.Action <System.Exception> onFail = null)
        {
            CachedAssetBundle ret = null;

            if (cachedAssetBundles.TryGetValue(assetBundleName, out ret))
            {
                if (ret.AssetBundle.State == CachedState.Cached)
                {
                    onSuccess(ret.AssetBundle.Value);
                    return(AsyncTaskInternal.Complete());
                }
                else
                {
                    var task = ret.AssetBundle.WaitForValueCreated(
                        this,
                        onSuccess,
                        onFail
                        );
                    return(task);
                }
            }
            else
            {
                CachedAssetBundle cachedAssetBundle = new CachedAssetBundle(
                    (s, f) => {
                    string assetbundleUri         = System.IO.Path.Combine(BaseUri.AbsoluteUri, assetBundleName);
                    AsyncTaskInternal downloading = new AsyncTaskInternal(assetbundleUri);
                    Http http = null;
                    if (!isManifest)
                    {
                        http = Http.GetAssetBundle(assetbundleUri, manifest.GetAssetBundleHash(assetBundleName), 0,
                                                   (www) => {
                            AssetBundle assetBundle = www.ToAssetBundle();
                            s(assetBundle);
                        },
                                                   f
                                                   );
                    }
                    else
                    {
                        http = Http.GetAssetBundle(assetbundleUri, 0, (www) => {
                            AssetBundle assetBundle = www.ToAssetBundle();
                            s(assetBundle);
                        }, f);
                    }
                    if (downloadingTask.Count == 0)
                    {
                        StartCoroutine(LoadAssetBundleQueue());
                    }
                    downloadingTask.Add(new KeyValuePair <Http, AsyncTaskInternal> (http, downloading));
                    return(downloading);
                },
                    onSuccess,
                    (ex) => {
                    cachedAssetBundles.Remove(assetBundleName);
                }
                    );
                cachedAssetBundles.Add(assetBundleName, cachedAssetBundle);
                return(cachedAssetBundle.AssetBundle.AsyncResult);
            }
        }