Problems with AssetBundle unloading

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.

324772–11448–$assetbundletest_777.zip (1.34 MB)

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.

Unity seriously need to fix this…

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 :frowning:

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.

1 Like

I ran into the same problem using Unity3.5.1f2 but the example project posted by Nevermind worked fine for me.

My problem turned out to be loading an asset bundle and the unloading it from Awake. Moving the function call to Start make it work fine

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!

Thanks,
Jason

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:

  1. Load level
  2. Load AB and store in to public static Dictionary<int, AssetBundle> AllAssetBundles;
  3. In OnDisable() I unload AB with AllAssetBundles[Id].Unload(true);
    ------> eheeeeee <--------------
  4. Load next level
    …

Probable Unload provides to hang when it called from some OnDisable() method.

The only solution is change behaviour to this

  1. Load level
  2. Unload AB from previous level with AllAssetBundles[Id].Unload(true);
  3. Load AB and store in to public static Dictionary<int, AssetBundle> AllAssetBundles;
  4. Load next level
  5. Unload AB from previous level
    …

I have encountered similar issue in u4.1.3.

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;

private void _UnloadBundle(string filePath, AssetBundle bundle, bool bUnloadLoadedObj)
{
    StartCoroutine(_DelayUnload(filePath, bundle, bUnloadLoadedObj));
}

private IEnumerator _DelayUnload(string filePath, AssetBundle bundle, bool bUnloadLoadedObj)
{
    Debug.Log(string.format("_DelayUnload: start: {0}", filePath));
    yield return 0;
    Debug.Log(string.format("_DelayUnload: execute: {0}", filePath));
    bundle.Unload(bUnloadLoadedObj);
}