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 proteinbeer · Apr 10, 2012 at 10:17 AM · texturememoryramunloadsingle

How to unload only 1 texture without a performance overhead?

Hi, I'm making a iphone rpg and finally came to meet memory doom and need to figure out a way to load and unload textures dynamically. The problem is I couldn't find a way to unload textures without stopping the game briefly and it does not look good at all.

I load texture of one item like below.

 function GetIcon(g : Manager) : Texture2D
 {   // generate instance if doesn't exist
     if(g.balance.itemInst[id] == null){
     
          // generate instance and set icon2D there
         var clone : Item = Instantiate(this, Vector3.zero, Quaternion.identity).GetComponent(Item);
         clone.icon2D = Resources.Load("Item Icon/" + class_ + '/' + gameObject.name, Texture2D);
         g.balance.itemInst[id] = clone; // set item instance
     
         clone.transform.parent = g.itemHolder; // group up
     }
     
     return g.balance.itemInst[id].icon2D; // from now on returns from instance
 }


I googled around and my only option seems to be Resources.UnloadUnusedAssets().

I tried Destroy(ItemGameObject) which holds the only referece to Texture2D and GC did not collect it (Some post says losing reference to texture will unload it and some post says GC does not handle textures which is confusing to newbie like me)

I experimented myself with really big texture so that I can clearly see if it is being loaded and unloaded and Resources.UnloadUnusedAssets() only worked.

It'd be much appreciated if experienced Unity programmer could help me on this one.

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
Best Answer

Answer by Kryptos · Apr 10, 2012 at 12:01 PM

To allow GC to release the memory, destroying the GameObject with Destroy(gameObject) may not be sufficent. You also need to release any reference to the resource.

For example, if you have a script with:

 public Texture2D myTexture;

The best way to do that is to use the OnDestroy() method:

 void OnDestroy()
 {
     this.myTexture = null;
 }

Resources.UnloadUnusedAssets() only release resources loaded with Resources.Load().

You can call GC explicitly with:

 System.GC.Collect();
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Texture2D pixels in system memory? 0 Answers

Should we enable Generate Mipmap for Normal Map? 1 Answer

Why a texture always consume twice RAM when running? 1 Answer

Why are compressed textures showing up huge in memory? 0 Answers

Assigning UV Map to model at runtime 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