- Home /
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
Answer by ThePunisher · Nov 21, 2013 at 11:13 PM
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.
Thanks for the reply.
Like you said, Unload(true) should unload the assets whether I have a reference to it or not. But if Unload(false) is called first then calling Unload(true), the texture will not be unloaded and still exist. This is why i am confused.
Well, you should be able to call Destroy() on the texture once you've kept the reference and called Unload(false). Have you tried that? $$anonymous$$ake sure it's the texture that is loaded from the asset bundle because I wouldn't want you to destroy one of your project's textures, lol.
If i call Destroy() on the texture, there is an error message. --> Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate(theObject, true);
Thanks.
Hmmm, I thought I remembered being able to call Destroy on a Texture2D but it must have been one that I created during runtime using new Texture2D(width, height). You could try explicitly unloading it using Resoures.UnloadAsset. Give that a shot and let me know if it does the trick.
Yes, Resources.UnloadAsset does the trick.
So I think that there are two options to release AssetBundle memory.
When asset is unused, call AssetBundle.Unload(true).
After www is done and reference the asset, call AssetBundle.Unload(false).
When asset is unused, clear all the references in the code, and call Resources.UnloadUnusedAssets().
And for option 1, until AssetBundle.Unload(true) is called, the file data of AssetBundle is kept in the memory.
Is it correct? Thanks.
Your answer
Follow this Question
Related Questions
How to import the object from server to unity 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Material doesn't have a color property '_Color' 4 Answers
Unhandled exception caused by AssetBundle downloading in Unity 4.1.2 0 Answers