Skip to content

Instantly share code, notes, and snippets.

@wmiller
Created February 3, 2014 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wmiller/8792588 to your computer and use it in GitHub Desktop.
Save wmiller/8792588 to your computer and use it in GitHub Desktop.
XML Database LoadAssetAsyncCo
private IEnumerator LoadAssetAsyncCo<T>(AssetInfo info, System.Action<T> onComplete) where T : UnityEngine.Object
{
if (info.AssetBundleInfoRef != null)
{
// Get the bundle from the loaded bundles collection
AssetBundle bundle;
if (!loadedBundles.TryGetValue(info.AssetBundleInfoRef.Entry.DatabaseID, out bundle))
{
throw new System.Exception("Asset bundle not found: " + info.AssetBundleInfoRef.Entry.Name);
}
// Async load the bundle. Yield until it is loaded.
AssetBundleRequest request = bundle.LoadAsync(info.Path, typeof(T));
yield return request;
// Except if there is no asset in the request
if (request.asset == null)
{
throw new System.Exception("Asset missing from bundle. Did you forget to rebuild?");
}
// Cast and return if the asset is the correc type.
if (request.asset is T)
{
if (onComplete != null)
{
onComplete(request.asset as T);
}
}
else
{
throw new System.Exception("Asset type mismatch");
}
}
else
{
throw new System.Exception("Invalid Asset Bundle Info");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment