Version: 2023.2
言語: 日本語
public void SetColor (string name, Color value);
public void SetColor (int nameID, Color value);

パラメーター

name プロパティーの名前
nameID Shader.PropertyToID によって検索されるプロパティーの nameID
value 設定する Color

説明

color 型のプロパティーを設定します

プロパティーブロックを追加します。すでに同じ名前の color 型のプロパティーがある場合、上書きされます。

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. using UnityEngine; public class ExampleClass : MonoBehaviour { public Mesh mesh; public Material material; private MaterialPropertyBlock block;

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

void Update() { // setup render params RenderParams rp = new RenderParams(material) {matProps = block};

// red mesh block.SetColor("_Color", Color.red); Graphics.RenderMesh(rp, mesh, 0, Matrix4x4.Translate(new Vector3(0, 0, 0)));

// green mesh block.SetColor("_Color", Color.green); Graphics.RenderMesh(rp, mesh, 0, Matrix4x4.Translate(new Vector3(5, 0, 0)));

// blue mesh block.SetColor("_Color", Color.blue); Graphics.RenderMesh(rp, mesh, 0, Matrix4x4.Translate(new Vector3(-5, 0, 0))); } }

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. using UnityEngine; public class ExampleClass : MonoBehaviour { public Mesh mesh; public Material material; private MaterialPropertyBlock block;

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

void Update() { // setup render params RenderParams rp = new RenderParams(material) {matProps = block};

// red mesh block.SetColor("_Color", Color.red); Graphics.RenderMesh(rp, mesh, 0, Matrix4x4.Translate(new Vector3(0, 0, 0)));

// green mesh block.SetColor("_Color", Color.green); Graphics.RenderMesh(rp, mesh, 0, Matrix4x4.Translate(new Vector3(5, 0, 0)));

// blue mesh block.SetColor("_Color", Color.blue); Graphics.RenderMesh(rp, mesh, 0, Matrix4x4.Translate(new Vector3(-5, 0, 0))); } }