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
1
Question by falantar14 · Nov 13, 2013 at 01:21 AM · buttonmaterialtoggle

Toggle material color and shader

I have a button that when clicked, finds a specific child(ren) and changes each material's color to green. Then, when clicked again, I would like for it to revert back to the original color. Considering there are multiple children that get effected, it seems impractical to store each original color in an array. Any ideas on how I can accomplish this? This is what I have so far:

     // Public variables
     public Transform mainObj;
     
     
     // Use this to highlight sections of obj
     public void HighlightObj (){
         // Loop through children to find highlightable sections
             foreach(Transform child in mainObj)
             {
                 if(child.name.Contains("selectable"))
                 {
                     foreach(Material mat in child.renderer.materials)
                     {
                         mat.color = Color.green;
                         mat.shader = Shader.Find("Unlit/Masked Colored");
                     }
                 }
             }
     }

I've tried using the UnityEditor PrefabUtility function to revert to prefab settings but I haven't had any luck.

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

Answer by GameVortex · Nov 14, 2013 at 12:56 PM

The PrefabUtility is not available in runtime, and objects do not have any prefab connections in runtime either, so reverting is not an option unfortunately.

Because you want to change the all materials on each renderer, there is not many ways around it other than storing them in an array. But instead of one huge array for all objects, you could maybe use a class for each selectable object that stores its values in its own array. Something like this:

 public class SelectableObject
 {
    private Color[] savedColors;
    private Shader[] savedShaders;
 
    public void Select()
    {
        savedColors = new Color[renderer.materials.Length];
        savedShaders = new Shader[renderer.materials.Length];
 
        for(int i = 0; i < renderer.materials.Length, i++)
        {
            savedColors[i] = renderer.materials[i].color;
            savedShaders[i] = renderer.materials[i].shader;
 
            renderer.materials[i].color = Color.green;
            renderer.materials[i].shader = Shader.Find("Unlit/Masked Colored");
 
         }
     }
 
     public void Deselect()
     {
         for(int i = 0; i < renderer.materials.Length; i++)
         {
             renderer.materials[i].color = previousColors[i];
             renderer.materials[i].shader = previousShaders[i];
         }
     }
 }
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 falantar14 · Dec 10, 2013 at 03:31 PM 0
Share

that helped, thanks!

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

17 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

Related Questions

How to change multiple objects' material (color) with one button? 2 Answers

My code works but produces an error in the console, not sure what's wrong but I would like to fix it. 1 Answer

UI Button should stay highlighted on click 2 Answers

change color with playerprefs using buttons, between scenes.,How to use playerprefs to save colors from a button? 3 Answers

Toggle Button Question 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