public void SetColor (string name, Color value);
public void SetColor (int nameID, Color value);

Parameters

nameThe name of the property.
nameIDThe name ID of the property retrieved by Shader.PropertyToID.
valueThe Color value to set.

Description

Set a color property.

Adds a property to the block. If a color property with the given name already exists, the old value is replaced.

The color value is considered to be always set in sRGB space and is converted to linear if the active color space is linear. You need manual updating of the color value if you switch between color spaces.

using UnityEngine;

// Draws 3 meshes with the same material but with different colors. public class ExampleClass : MonoBehaviour { public Mesh mesh; public Material material; private MaterialPropertyBlock block;

void Start() { block = new MaterialPropertyBlock(); }

void Update() { // red mesh block.SetColor("_Color", Color.red); Graphics.DrawMesh(mesh, new Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block);

// green mesh block.SetColor("_Color", Color.green); Graphics.DrawMesh(mesh, new Vector3(5, 0, 0), Quaternion.identity, material, 0, null, 0, block);

// blue mesh block.SetColor("_Color", Color.blue); Graphics.DrawMesh(mesh, new Vector3(-5, 0, 0), Quaternion.identity, material, 0, null, 0, block); } }

Function variant that takes nameID is faster. If you are changing properties with the same name repeatedly, use Shader.PropertyToID to get unique identifier for the name, and pass the identifier to SetColor.

using UnityEngine;

// Draws 3 meshes with the same material but with different colors. public class ExampleClass : MonoBehaviour { public Mesh mesh; public Material material; private MaterialPropertyBlock block; private int colorID;

void Start() { block = new MaterialPropertyBlock(); colorID = Shader.PropertyToID("_Color"); }

void Update() { // red mesh block.SetColor(colorID, Color.red); Graphics.DrawMesh(mesh, new Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block);

// green mesh block.SetColor(colorID, Color.green); Graphics.DrawMesh(mesh, new Vector3(5, 0, 0), Quaternion.identity, material, 0, null, 0, block);

// blue mesh block.SetColor(colorID, Color.blue); Graphics.DrawMesh(mesh, new Vector3(-5, 0, 0), Quaternion.identity, material, 0, null, 0, block); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961