Problem with AssetBundle unloading and mainAsset

Hi! I have a problem with AssetBundle.

When using WWW to load an AssetBundle and get a object from AssetBundle.mainAsset, are there any side effects to call function AssetBundle.Unload(false) to free memory?
It seems like that if function Unload(false) is called, the object won’t be destroyed when the function AssetBundle.Unload(true) is called.

ps. The mainAsset is a Texture2D in my test case.

Example code:

public class TestUnload : MonoBehaviour {

	void Start () {
		StartCoroutine(TestLoad());
	}
	
	IEnumerator TestLoad() {
		string BundleURL = "file://" + Application.streamingAssetsPath + "/texture.assetbundle";

		using (WWW www = new WWW(BundleURL)) {
			yield return www;

			m_testObject = www.assetBundle.mainAsset;

			www.assetBundle.Unload(false);

			// wait 2 seconds for test
			yield return new WaitForSeconds(2.0f);

			www.assetBundle.Unload(true);
		}

		if (m_testObject != null)
		{
			Debug.Log("m_testObject != null");
		}
		else
		{
			Debug.Log("m_testObject == null");
		}
	}

	Object m_testObject = null;
}

if www.assetBundle.Unload(false) is called → m_testObject != null

if www.assetBundle.Unload(false) is not called → m_testObject == null

Thanks

I’m not entirely sure what you’re question is really getting at, but this is the way the method differs when passing true or false.

Unload(false):

If you load an asset from an asset bundle and you store a reference to it. Then calling .Unload(false) will only unload assets which you don’t have a reference to.

In other words, the following will not unload your texture (since you have a reference to it):

m_testObject = www.assetBundle.mainAsset;
www.assetBundle.Unload(false);

Unload(true):

If you load assets from an asset bundle and call Unload with true as a paramter then it will unload the assets whether you have a reference to it or not. Therefore, if you have a reference to the asset then the reference will become null.

In other words, you load a texture and apply it to some GameObject. Calling Unload(true) will cause you’re GameObject to lose the texture.