Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

c# - Find and change property name in a shader

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...