在着色器中查找和更改属性名称 [英] Find and change property name in a shader

查看:99
本文介绍了在着色器中查找和更改属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设置新材质后,我想更改并设置3件事:

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

首先将着色器类型更改为不亮/彩色"

First The shader type to Unlit/Color

第二个反照率颜色,例如将其更改为:255,0,0,255

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

第三种金属值,从0到1

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 ()
    {

    }
}

此行不执行任何操作:

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

这就是我要更改的内容:

This is what I want to change:

推荐答案

当您需要更改着色器属性,但又不知道要使用的专有名称时,请选择材质,然后单击其设置图标然后点击选择着色器" .从那里,您将看到所有着色器属性名称及其类型,并且您将知道用于更改着色器属性的函数和名称.

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.

使用默认的标准着色器,它看起来像这样:

With the default standard shader it looks something like this:

您需要知道这一点,否则您将需要为每个要更改的属性询问新问题.

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

您的渲染器参考:

public Renderer rend;

要设置它,请使用SetXXX函数:

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

要获取它,请使用GetXXX函数:

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

将反照率颜色更改为255,0,0,255.

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

请注意,我使用Color32而不是Color,因为Color32接受的值介于0255之间,而Color期望的值介于01之间.

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

要将材质的着色器更改为"Unlit/Color" ,只需找到它,然后将其分配给材质即可:

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;

请注意,不亮/彩色" 着色器不具有_Metallic属性.它具有一个属性,即"_ Color" .您可以使用我描述的第一种方法来确定其具有的属性,然后再尝试更改它们.

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.

要使用相同的材​​质更改所有对象,请使用Renderer.sharedMaterial而不是Renderer.material.共享材质会更改原始材质,并且只要您尚未在材质上调用Renderer.material,其他所有对象都应拾取该新材质,而该材质实际上为该材质创建了新副本并断开了渲染器材质与原始材质的连接.

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.

这篇关于在着色器中查找和更改属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆