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
1
Question by Le_nain · Sep 26, 2011 at 01:37 PM · iosmaterialmemorychange material

memory consumption when changing materials [iOS]

Hi guys, I have a problem which drives me crazy about material swapping.

[CONTEXT] This happens on iOS. Not saying it doesn't happen on other platforms, just that this is the one I'm testing with. Imagine a chess game; the board is composed of multiple tiles, the user being able to touch them in order to select them. Depending on which tile is selected and what the user is allowed to do with it (attack, move, etc.) the tiles change their material in order to give the user a visual feedback.

[PROBLEM] I'm running my game on an iPad, using Xcode's Instruments to follow the memory consumption. I noticed that every time I touch a tile and thus change its material some new memory is allocated. I don't get why, because all I do is

 if (.....)
    renderer.material = attackMat;
 else if (...)
    renderer.material = validMoveMat;

where attackMat and validMoveMat are some standard Unity Material already assigned at scene start.

The 2nd point is that this memory allocation occurs only the 1st time I touch each tile: if I touch tile A1 some memory is allocated and its material is changed; if I touch it again (whenever, not necessarily justa after the first touch) its material will be changed again, but no more memory allocation (even if the new material is not one of the 2 already used).

I ran some extensive tests and am pretty sure now that the memory is allocated for that purpose, I just don't understand why and how to avoid it.

Any ideas?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ArmarageX · Jan 20, 2013 at 12:13 AM

My guess is that the engine "batches" your similar textured objects in the beginning.... and sees that "Oh you have black and white tiles.... let me create 2 memory slots for you"...

And when you introduce the new material.... it will go "Oh so you actually needed one more.... let me give you a total of 3 slots to play with"

And thus from then on... you'll be running on 3... and it wont reduce back down even when you revert back to the original material.. (Metaphorically speaking)

Comment
Add comment · Show 1 · 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 Bunny83 · Jan 20, 2013 at 01:21 AM 0
Share

x) Just realised that this question is from 2011...

Anyway, now it has some answers and might help others.

avatar image
0

Answer by Bunny83 · Jan 20, 2013 at 01:19 AM

Right, just read the docs on Renderer.material and don't skip the "important note".

Whenever you read or write to renderer.material, Unity will duplicate the material so this renderer has it's own instance. Once a renderer has a unique instance it will keep it.

This will actually break any batching since each tile you click will now have a unique material. What you actually want to do is this:

 if (...)
     renderer.sharedMaterial = attackMat;
 else if (...)
     renderer.sharedMaterial = validMoveMat;

"sharedMaterial" will return a reference to the shared Material instance. When you set the sharedMaterial Unity will save the reference and will not duplicate the material.

Keep in mind that since the different objects share the same material, any changes on the material itself will affect all objects that use this material.

Note by change on the material i mean things like:

 attackMat.color = xxx;
 attackMat.mainTexture = xxx;
 attackMat.shader = xxx;
 attackMat.SetXXX(...);



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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Best way to manage hundreds of materials? 1 Answer

Do I need to manually destroy material/model instances that are lazily copied? 2 Answers

unexpected massive memory allocation on iOS while loading maps 1 Answer

Is it possible to find out how many virtual memory left free on iphone of my user? 1 Answer

Does assetbundle work well with mobile devices? 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