Monday, May 11, 2015

A problem with Unity3D and downloading/caching AssetBundles

(This post is in reference to Unity 5.0.0f4.)
Anyone who has struggled with Unity’s asset bundle solution may have faced these issues, and hopefully can provide a solution.  Here is what I want out of the APIs surrounding Unity’s asset bundle system, specifically, the discrete abilities to:
- Download an asset bundle
- Load an asset bundle
- Cache an asset bundle
- Check if an asset bundle is cached

But instead, as is all too common with Unity’s APIs, we get bits and pieces of this hobbled together in a way that seems to assume a very specific (and often not ideal) workflow.  Here’s essentially what Unity offers via API:

- Load an asset bundle from cache, or download it if it’s not in the cache and be forced to load it if you want to use it
- Download an asset bundle but don’t cache it
- Check if an asset bundle is cached
Long story short, I cannot do what I need to do with these APIs.  So instead I need to create a convoluted façade over them which results in much more work than should be required.  Further adding a [very large] wrinkle to the problem is that the loading of an asset bundle is intimately tied to the download operation; namely, if I want to use an asset bundle that I’ve just downloaded, I need to load it right away from the download code (since an asset bundle is loaded via accessing the assetBundle property of WWW--no joke).  I can’t easily download it, pass it along to another object, and then load it when I’m ready without worrying about the IDisposable nature of the WWW object.

If any of what I’ve said sounds like nonsense, I’d love to hear the solutions to these issues.