- Home /
yield on WWW? or AssetBundleRequest? or both?
I'm trying to write a generic manager for all downloads, and I really want to understand what asynchronous and what's blocking.
I wrote the following and it worked:
WWW www = new WWW(mUrl);
yield return www;
AssetBundleRequest request = www.assetBundle.LoadAsync("Cube", typeof(Object));
yield return request;
Instantiate(request.asset);
But so does this (I don't seem to need to yield on www):
WWW www = new WWW(mUrl);
AssetBundleRequest request = www.assetBundle.LoadAsync("Cube", typeof(Object));
yield return request;
Instantiate(request.asset);
Is the application blocking in the www.assetBundle getter? Or is this equivalent to the former (i.e. the download occurs during the yield on request)?
Adding my 2 cents from 2018, You can also listen to .completed event inside the AssetBundleRequest object.
request.completed += (operation)=> { /do stuff/ };
Also you should not forget to Dispose the WWW object,
And its wierd that its letting you Instantiate something of type Object
Answer by Lucas Meijer 1 · Jan 09, 2010 at 03:43 PM
There's several things going on here.
- The download of the bytes of the assetbundle
- The creation of an assetbundle object from those bytes
- The creation of the Cube object, based on the description of the cube in the assetbundle.
Step 1 and 3 you are yielding on. As Jonas points out that's a good idea. Step 2 we actually don't (yet) allow you to do asynchronously. This is not a big thing, as it's typically a very fast step, that doesn't cause a hickup.
Whenever you're using AssetBundle's it's good practice do use the Asynchronous API to load objects from them, as if you do so, the loading process happens on a seperate thread, which is tech speak for "you wont get a hickup". This API is new since Unity2.6, there's a similar api for loading entire scenes/levels.
Bye, Lucas
I'm really confused with the WWW.assetBundle property. It does some decompression and returns an AssetBundle object. But nothing inside it is "Loaded" yet, right? Until we Load()
assets from the AssetBundle. So is it simply "all uncompressed bytes in RA$$anonymous$$, ready for your loading"? And then there's Unload(false)
to unload the compressed bytes from RA$$anonymous$$. So if we never call this, those stay in RA$$anonymous$$, but would there ever be a conceivable benefit in keeping those in RA$$anonymous$$?.....
Answer by jonas-echterhoff · Jan 09, 2010 at 01:12 PM
You are correct. The latter code snippet works, because the application blocks in the WWW.assetBundle getter. The first code snippet is preferable, as it lets your game continue while the data is downloaded.