- Home /
UGUI button gray shader
I have a shader which has a property named '_Gray' to control whether button is gray or not (simply ( r + g + b ) / 3)
I have 4 buttons using this shader.
in Update function, i do this for these 4 buttons:
 void Update()
     {
         currGray = !btn.interactable;
         btn.targetGraphic.material.SetFloat("_Gray", currGray ? 1f : 0f);
 
             if (txt != null)
                 txt.material.SetFloat("_Gray", currGray ? 1f : 0f);
     }
Result: these 4 buttons are all gray, or none of them is gray.
Question: Seams I have to change material's property just before rendering the button, so where to put that code? I've read MonoBehaviour document.
Or do i have to assign different material to each button? how ?
Thanks
Answer by StrongGuy · Jan 19, 2016 at 01:55 AM
This is what I am doing now, seems it works good: using UnityEngine; using UnityEngine.UI; using System.Collections;
 [RequireComponent(typeof(Button))]
 public class GrayButton : MonoBehaviour
 {
     bool currGray = false;
     Button btn = null;
     Text txt = null;
 
     Material matGrayBtn = null;
     Material matGrayTxt = null;
 
     void Start()
     {
         btn = GetComponent<Button>();
         txt = GetComponentInChildren<Text>();
 
         currGray = !btn.interactable;
 
         // clone a material
         matGrayBtn = (Material) Instantiate (btn.targetGraphic.material
                                              //ResourceManager.Instance.LoadMainObject("Assets/Bundles/Shaders/UIDefaultGray.mat")
                                              );
         btn.targetGraphic.material = matGrayBtn;
         if (txt != null)
         {
             matGrayTxt = (Material) Instantiate (txt.material
                                                  //ResourceManager.Instance.LoadMainObject("Assets/Bundles/Shaders/UIDefaultFontGray.mat")
                                                  );
             txt.material = matGrayTxt;
         }
 
         Set(currGray);
     }
 
     void Set(bool gray)
     {
         matGrayBtn.SetFloat("_Gray", gray ? 1f : 0f);
         if (matGrayTxt != null)
             matGrayTxt.SetFloat("_Gray", gray ? 1f : 0f);
     }
 
     void Update()
     {
         // detect interactable change
         bool gray = !btn.interactable;
         if (gray != currGray)
         {
             currGray = gray;
             Set(currGray);
         }
     }
 }
 
Your answer
 
 
             Follow this Question
Related Questions
Why do materials created at runtime show up incorrectly in the inspector? 1 Answer
Bast way to combine multiple materials into one 0 Answers
Can't find unlit shader in material shader selection!, 1 Answer
How can i make a shader to get this effect? 0 Answers
Shader: get back scene pixel color? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                