# How can an asset bundle's resources be destroyed after destroying the compressed bundle data?

Using:

AssetBundle.Unload( false );


Will clear the compressed asset bundle data. This, unfortunately, destroys the bundle object as well, which means nothing is keeping track of the loaded resources.

Using:

AssetBundle.Unload( true );


Will clear all resources correctly. However, it may be desirable to load the required resources, free the compressed data, and then free the loaded resources at a later time. How can this be done?

When unloadAllLoadedObjects is false, compressed file data for assets inside the bundle will be unloaded, but any actual objects already loaded from this bundle will be kept intact. Of course you won't be able to load any more objects from this bundle.

When unloadAllLoadedObjects is true, all objects that were loaded from this bundle will be destroyed as well. If there are game objects in your scene referencing those assets, the references to them will become missing.

In your case, you want to use

AssetBundle.Unload(false)


This will destroy the loaded assets, but keep intact the already created GameObjects.

These being just GameObjects, you can destroy them at any later time using Object.Destroy.

public static void Destroy(Object obj, float t = 0.0F);


PS: You should take a look at Resources.UnloadUnusedAssets too.

As the function name implies, it Unloads assets that are not used.

An asset is deemed to be unused if it isn't reached after walking the whole game object hierarchy, including script components. Static variables are also examined.

• But the loaded object is a prefab, right? When will this be destroyed? If I load a prefab from an asset bundle, instantiate the prefab, then call AssetBundle.Unload( true ), the instantiated object is missing all it's resource references. If I instantiate the loaded prefab and then call AssetBundle.Unload( false ), followed by using DestroyImmediate on the loaded prefab, the instantiated object is completely in tact. This seems to imply using destroy on the loaded object does not free the resources it uses( meshes, textures, etc )
– Ben
Jun 22, 2015 at 9:56
• Those will eventually be cleaned up when Unity decides to do it's own Garbage Collecting. However, it can be forced programmatically too. Have a look at: docs.unity3d.com/ScriptReference/… Jun 22, 2015 at 10:05
• But the assets are in use, on the scene object that was instantiated. So, Resources.UnloadUnusedAssets does not destroy them in this case, but AssetBundle.Unload( true ) does.
– Ben
Jun 22, 2015 at 10:18
• You're losing me Ben. Why do you want to destroy something that you want to remain in use? Resources.UnloadUnusedAssets will clear up memory after the GameObjects referencing those assets get destroyed. Jun 22, 2015 at 10:27
• It would be nice to have something be explicitly destroyed so entire assets can't be accidentally left in memory due to a single reference. But you're right, this will work, it's just a bit more fragile.
– Ben
Jun 22, 2015 at 10:33