Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yoyocyhsieh · Nov 21, 2013 at 07:35 AM · assetbundle

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yoyocyhsieh · Nov 22, 2013 at 01:24 AM 0
Share

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.

avatar image ThePunisher · Nov 22, 2013 at 04:08 AM 0
Share

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.

avatar image yoyocyhsieh · Nov 25, 2013 at 11:58 AM 0
Share

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.

avatar image ThePunisher · Nov 25, 2013 at 07:48 PM 0
Share

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.

avatar image yoyocyhsieh · Nov 28, 2013 at 10:31 AM 0
Share

Yes, Resources.UnloadAsset does the trick.

So I think that there are two options to release AssetBundle memory.

  1. When asset is unused, call AssetBundle.Unload(true).

  2. After www is done and reference the asset, call AssetBundle.Unload(false).

  3. 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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges