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 hjenkinz · Aug 08, 2017 at 03:42 PM · materialefficiencymaterial colorbest practice

Changing the colour of many objects at once (efficient material instancing)

I have 250+ objects in a grid pattern on screen that change colour depending on the current audio (like a flashing disco floor). I want to be able to change the colour of each objects material every tick at runtime. To do this I obviously have to return an instance of the material, which gives me 250+ instances of a material being looped through every tick, with changes to their colour. This drops my frame rate from 900~ to 350~.

Is there a more efficient way to be able to change the colour of many objects at once?

My code to run through them is:

  for (int a = 0; a < hexArray.Length; a++)
             {
                 GameObject[] curRow = hexArray[a];
 
                 for (int b = 0; b < curRow.Length; b++)
                 {
                     GameObject grabHex = curRow[b];
 
                     if (grabHex != null)
                     {
                         Material material = grabHex.GetComponent<Renderer>().material;
                         //set colour
                         float colNum = AudioScript.amplitudeBuffer;
                         colNum = Mathf.Clamp(colNum, 0f, 1f);
 
                         Color newCol = new Color(colNum, colNum, colNum);
                         material.SetColor("_Color", newCol);
                         material.SetColor("_EmissionColor", newCol);
                     }
                 }
             }
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 ShadyProductions · Aug 08, 2017 at 05:08 PM

Cache your material's for each gameobject in a dictionary so you don't have to use getcomponent every frame, but only once for each gameobject.

 private Dictionary<int, Material> _materialCache = new Dictionary<int, Material>();

Implement it like this:

     if (grabHex != null)
     {
         Material material;
         if (!_materialCache.TryGetValue(grabHex.GetInstanceID(), out material))
         {
             material = grabHex.GetComponent<Renderer>().material;
             _materialCache.Add(grabHex.GetInstanceID(), material);
         }

         //set colour
         float colNum = AudioScript.amplitudeBuffer;
         colNum = Mathf.Clamp(colNum, 0f, 1f);

         Color newCol = new Color(colNum, colNum, colNum);
         material.SetColor("_Color", newCol);
         material.SetColor("_EmissionColor", newCol);
     }
Comment
Add comment · Show 2 · 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 hjenkinz · Aug 08, 2017 at 07:51 PM 0
Share

Thank you for your answer, it works very well and got me an fps increase of 10. I wonder if there are any other optimisations I could make, maybe something on the shader side?

avatar image ShadyProductions hjenkinz · Aug 08, 2017 at 08:20 PM 0
Share

Not sure about the shaders, but it might be worth to note that you create a new object instance of color each time. $$anonymous$$aybe it might help performance if you create 1 instance, and simply change the values of the instance each time ins$$anonymous$$d of making a whole new color object every single frame.

 // Set it outside of method
 private Color newCol = new Color();

 // Set this in loop
 newCol.r = colNum;
 newCol.g = colNum;
 newCol.b = colNum;

 //assign new values
 material.SetColor("_Color", newCol);
 material.SetColor("_EmissionColor", newCol);

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

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

Unet Material color change do not work. 0 Answers

Material color changing in Photon Multiplayer scenes 0 Answers

Getting value of material and applying it to another material through code 1 Answer

Material alpha is 1 but still see through 0 Answers

How to check if two objects share the same material within an if statement? 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