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)?
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
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.