Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 xpavelos · Dec 21, 2016 at 07:57 PM · materialmemory-leaksprite renderermemory leak

How to change material without memory leaks?

Hey all.

After searching for an answer I have learned that materials are handled a little different way. So now I know that each time the material property is accessed there is a copy of it created and that's what causes the leak. I tried to cache material before changing it with no success.

This is the original code I used (without caching):

         SpriteRenderer sr;
         float flashDurationLeft;
 
         void Awake()
         {
             sr = GetComponent<SpriteRenderer>();
         }
 
         void FixedUpdate()
         {
             if (flashDurationLeft > 0)
             {
                 if (sprite.material != GameManager.instance.enemyHit_mat)
                     sprite.material = GameManager.instance.enemyHit_mat;
 
                 flashDurationLeft -= Time.deltaTime;
 
             } else if (flashDurationLeft <= 0)
             {
                 if (sprite.material != GameManager.instance.defaultSprite_mat)
                     sprite.material = GameManager.instance.defaultSprite_mat;
             }
         }

How would you solve this problem?

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 Geometrical · Dec 21, 2016 at 11:39 PM

It's simple, just use the SharedMaterial property as the Material property is instance specific.

 sprite.sharedMaterial
Comment
Add comment · Show 3 · 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 xpavelos · Dec 21, 2016 at 11:54 PM 0
Share

Worked like a charm, many thanks. But I am now quite confused how it works.

avatar image Geometrical · Dec 22, 2016 at 12:17 AM 0
Share

@xpavelos it's very simple. If you want to modify the material on an object without actually effecting the material file in the Unity Asset Database (the project's asset folder), you'll use the $$anonymous$$aterial property since it's object instance specific (meaning that the material will be cloned before it's given to the user for the current instance of the object). You'd use the Shared$$anonymous$$aterial property if changes to the the actual material file was intended.

avatar image xpavelos Geometrical · Dec 22, 2016 at 10:19 AM 0
Share

I get it. Thank you.

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

68 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 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 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 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

editor script memory leak 1 Answer

EXC_BAD_ACCESS (SIGABRT) from Unity::Material::CreateMaterial 0 Answers

Normal map not showing correctly on 2d sprite 0 Answers

if material is not destroyed in OnDestroy, Does renderer.material.SetFloat("_alpha",0.1f) cause memory leak ? 0 Answers

webgl memory leak 1 Answer


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