- Home /
Issues with WWW.LoadFromCacheOrDownload
We're having issues with caching of AssetBundles in iOS. We download the manifest, then download and load all of the AssetBundles in the manifest before the game starts. Everything is downloaded properly and the game works fine.
Next time we launch the game, however, some of the using WWW.LoadFromCacheOrDownload (http://docs.unity3d.com/ScriptReference/WWW.LoadFromCacheOrDownload.html), with the exact same version as before (same version is used for all the AssetBundles), 90% of the AssetBundles are loaded from cache, but a few are downloaded again, even though nothing changed in the bundles, their version, or the version we provide. Has anyone ever encountered this? Any solutions?
According to unity docs the limit is 4GB: http://docs.unity3d.com/ScriptReference/Caching-maximumAvailableDiskSpace.html
Answer by pitimoi · Jul 08, 2015 at 08:39 PM
Hi Tomer,
According to the Unity LoadFromCacheOrDownload documentation :
If the cache folder does not have any space for caching additional files, LoadFromCacheOrDownload will iteratively delete the least-recently-used AssetBundles from the Cache until sufficient space is available to store the new AssetBundle. If making space is not possible (because the hard disk is full, or all files in the cache are currently in use), LoadFromCacheOrDownload() will bypass Caching and stream the file into memory like a normal "new WWW()" call.
Then yes, it is possible that some assetbundle will be downloaded again if the cache does not have enough space to download everything.
Edit :
If you want to make sure that your assetbundle will always be available while the player is offline, the only way to go is to NOT use the LoadFromCacheOrDownload on assetbundle from the server and download them yourself. Here is how I did it on my side :
Build your assetbundle and create a json file (or xml or whatever...) which contains for each assetbundle the version number and a md5 hash of its binary.
When the player launch the game, read the json file from your server and compare the version number between the player saved game and the server current value. If it is the same, check the md5 hash (just to be sure it is nor corrupted).
If the version from the server is newer or the md5 hash differs, download the package from the server using
new WWW()
and save it as the new version. You should be able to save it in the Application.persistantDataPath but be careful on iOS with the cloud save game which may try to save all the assetbundle in the cloud (it will be a cause of failure while submitting if so).When everything is done, the player got a downloaded version of each assetbundle and a file (json, xml, encrypted binary...) which contains the current version of each assetbundle in its saved game directory. At this point, you can use the LoadFromCacheOrDownload using your local path and the current assetbundle version to use all your assetbundle like if you were online, avoiding any caching limitation.
This solution does not works for a WebPlayer build because you cant save any binary file on the disk with this build target
Thanks, but this is not the case, there is plenty of disk space and we're not near the 4GB limit.
You can still have a look on the Caching class which may have helpful methods for you (like the expirationDelay value).
Because of this kind of problems I don't rely on the LoadFromCacheOrDownload method anymore to use my assetbundle and created a lot of workarounds to avoid it.
The only place where I still use LoadFromCacheOrDownload is with WebPlayer because we don't have any choice, and we don't really take care on how many times an assetbundle will be re-downloaded ins$$anonymous$$d of being re-used from the cache on this platform.
Yeah, I guess at its current state, writing your own caching mechanism is the way to go... If you can edit your answer and explain how you created your caching mechanism I can accept the answer so others can see.
I updated my answer with more informations about how I handle this problem, thanks :)
Thanks. If/when we end up implementing this I'll let you know how it goes.
Your answer
Follow this Question
Related Questions
how to delete specific files from cache? 3 Answers
LoadFromCacheOrDownload is sensibly slower than WWW, why? 1 Answer
How to determine validity of cached asset bundle 1 Answer
Progress when using WWW.LoadFromCacheOrDownload 2 Answers
Preloading asset bundles by unloading them after download - doesn't work with unity cache 3 Answers