I have found a very strange behaviour when unloading asset bundles.
I have bundles which contain textures, and bundles which contain animations (actually, a model with a single animation). These bundles are loaded using WWW, then I get the main asset and unload the bundle. The code looks like this:
AssetBundle ab = [url]www.assetBundle;[/url]
ab.LoadAll();
m_Texture = (Texture2D)ab.mainAsset;
// or in case of animation
//m_Clip = ((GameObject)ab.mainAsset).animation.clip;
ab.Unload(false);
Everything works, until I try to load another scene - at this moment the editor hangs up. I tried building a Windows standalone app, and it crashes when trying to load another scene; however, Unity iPhone seems to be working OK.
This crash is caused by unloading the asset bundles; if I comment out the call to ab.Unload(), everything works fine (except these bundles take up memory, and can’t be re-loaded again). Even weirder, if I unload the texture bundle, everything still works fine - it’s the animation bundle that’s causing the problem!
What is going on here? Is this a bug in Unity or am I doing something wrong? If it’s a bug, maybe there’s some workaround short of keeping all bundles loaded forever?
I have created a very simple test project to show this, and I’m attaching it to this post.
I have just experienced the exact same problem. If I call www.assetbundle.Unload(false) Unity crashes after I stop running the game in the editor. The only item in the bundle that I load is an AnimationClip.
I too suffered grievously from this problem, but found a workaround!
I was able to prevent Unity 3.1 from crashing by storing a static reference to all my loaded assetbundles, loading a new scene so EVERYTHING would be deleted, and then clearing them.
I somewhen stop using unload false. Either I want to keep it around or I kill it at the point I want it to be gone with Unload true … unload false only lead to ugly problems with bundles that overlap prefabs for one or more reasons, cause Unitys AssetBundle system is stupid enough to just refuse load the other bundle instead of load the missing parts
Unity was crashing whether I unloaded with true or false, but after finding the workaround I set it to unload true. I agree, unload false doesn’t make too much sense.
Can you explain in a bit more detail how this workaround works? I’m finding the same issue where my game runs, loads the asset bundle, calls unload(), and then when I stop playing the Editor hangs/crashes.
I read what you said about static references, but I’m a bit new to Unity and am not sure what you meant. I would love to learn your workaround though!
Same problem with 3.5.6.f4, Unity friezes just in time, when I unload AB, no matter true/false parameter. Frieze just before next step to load next level, but I think load level was not called at all.
Workflow:
Load level
Load AB and store in to public static Dictionary<int, AssetBundle> AllAssetBundles;
In OnDisable() I unload AB with AllAssetBundles[Id].Unload(true); ------> eheeeeee <--------------
Load next level
…
Probable Unload provides to hang when it called from some OnDisable() method.
The only solution is change behaviour to this
Load level
Unload AB from previous level with AllAssetBundles[Id].Unload(true);
Load AB and store in to public static Dictionary<int, AssetBundle> AllAssetBundles;
I loaded a TextAsset from bundle, read it, then unload the bundle, all in one function, and unity editor froze;
I have tried commented out the unload part, then everything went well;
The workaround I take is to use a Coroutine to postpone the unload to next frame, then it works, at least for now;