Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and change property name in a shader

Tags:

c#

unity3d

I want to change and set 3 things after setting the new material:

First The shader type to Unlit/Color

Second The albedo color to change it for example to: 255,0,0,255

Third The metallic value from 0 to 1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoorsLockManager : MonoBehaviour
{
    public bool locked;
    public Color lockedColor = Color.red;
    public Color unlockedColor = Color.green;
    public Renderer rend;

    private GameObject[] doorPlanes;

    private void Start()
    {
        doorPlanes = GameObject.FindGameObjectsWithTag("DoorPlane");

        for (int i = 0; i < doorPlanes.Length; i++)
        {
            rend = doorPlanes[i].GetComponent<Renderer>();
            if (locked)
            {
                rend.material.SetFloat("Metallic", 1);
                rend.material.color = lockedColor;
            }
            else
            {
                rend.material.color = unlockedColor;
            }
        }
    }

    // Update is called once per frame
    void Update ()
    {

    }
}

This line does nothing:

rend.material.SetFloat("Metallic", 1);

This is what I want to change:

Material/Shader

like image 808
Benzi Avrumi Avatar asked Mar 05 '23 05:03

Benzi Avrumi


1 Answers

When you need to change a shader property but you don't know what to use for the proper name to use, select the material, click on its settings icon then click on "Select Shader". From there, you will see all the shader property names and their types and you will know which function and name to use to change the shader's properties.

enter image description here

With the default standard shader it looks something like this:

enter image description here

You need to know this otherwise you would need to ask new question for each property you want to change.

Your Renderer reference:

public Renderer rend;

To set it, the SetXXX function is used:

rend.material.SetFloat("_Metallic", 1); //Metallic is a float

To get it the GetXXX function is used:

float metallic = rend.material.GetFloat("_Metallic"); //Metallic is a float

To change or get the The albedo color to 255,0,0,255.

rend.material.SetColor("_Color", new Color32(255, 0, 0, 255));
Color color = rend.material.GetColor("_Color");

Notice that I used Color32 instead of Color because Color32 takes values between 0 and 255 while Color expects values between 0 and 1.

To change the material's shader to "Unlit/Color", just find it then assign it to the material:

Shader shader = Shader.Find("Unlit/Color");
rend.material.shader = shader;

Note that the "Unlit/Color" shader doesn't have the _Metallic property. It has one one property and that is "_Color". You can use the first method I described to determine which properties it has before attempting to change them.

What if the shader is used on many different objects. I want all the objects to change when the property is changed on one of them.

To change all the objects using the-same material, use Renderer.sharedMaterial instead of Renderer.material. The shared material changes the original material and every other objects should pick that new material up as long as you have not called Renderer.material on the material which actually makes a new copy for the said material and disconnects the renderer material from the original material.

like image 140
Programmer Avatar answered Mar 09 '23 06:03

Programmer