I'm trying to preload asset bundles into the unity cache at runtime and also keep the memory footprint low, so I want to unload them as soon as they have been downloaded.
The problem I'm seeing is that once an asset bundle has been loaded from the cache, if it is unloaded and then loaded again, the WWW.LoadFromCacheOrDownload method will never finish. Sometimes it will load one or two files and then stop working. By never finish and stop working I mean that the WWW.isDone and WWW.progress never budge from false/0.
There doesn't seem to be any documentation for the Caching object nor the WWW.LoadFromCacheOrDownload method and there is barely a mention of either on the forums or anywhere I can find.
I've been up all night trying to find a workaround for this. I've been testing this in the editor, so perhaps it is a problem with just the editor's usage of the cache. One theory I have is that something is holding open file handles so that when the cache file is requested again it can't open it. I've also been able to get the editor to hang or crash doing things to WWW.assetBundle, even referencing it after calling Unload on it, or not calling Dispose on the WWW object or calling it in some improper order. The consequence is that the unity editor process will hang with processor usage at 0%. I changed some stuff to delay calling AssetBundle.Unload until the next frame which makes me think there is some re-entrancy problem with immediately unloading a bundle after referencing the WWW.assetBundle. Just touching WWW.assetBundle does some funky things itself. Doing if(WWW.assetBundle != null) will sometimes cause an exception to be thrown.
If anybody has some info on caching, I'd really appreciate it.
A couple of updates:
I changed the WWW download wait to the following and it cleared up a lot of the hanging issues:
I also put in a wait timer after calling either Caching.DeleteFromCache or Caching.CleanCache to make sure the operation has time to complete before attempting to load any other assets through WWW.LoadFromCacheOrDownload. My theory was that if the cache deletion was delayed at all (perhaps due to open file handles or whatever), that an immediate load and subsequent write into that same cache directory would then get clobbered by the delayed cache delete, causing the cache to go into an inconsistent state.
answered Jul 23 '10 at 02:06 AM
Note that the WWW APIs (both in Cached or Uncached forms) are not completely asynchronous. That means that doing
will result in an infinite loop, at least when used on an asset bundle. The reason is that Unity needs some time in it's game loop to integrate download objects from the AssetBundle, which cannot all be done on a thread. So, you need to give back control to Unity while waiting for the download to finish.
The preferred way of doing that (as you seem to have found out yourself), is to do a
, which will resume your script when the download is finished.
answered Jul 23 '10 at 08:42 AM
jonas echterhoff ♦♦