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); }
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))); }
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); }
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); } }