SetColor
Set a color property.
Read time 4 minutesLast updated 6 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
SetColor(string, Color)
Set a color property.
public void SetColor(string name, Color value)
Parameters
Remarks
Adds a property to the block. If a color property with the given name already exists, the old value is replaced.
The method assumes that the color value is in sRGB space. The method converts the value to linear color space if the active project color space is linear.
if you switch between color spaces in your project, Unity does not convert color values automatically. You need to handle the conversion yourself.
The method does not support setting HDR colors directly.
SetColorTo set HDR colors, use MaterialPropertyBlock.SetVector instead, passing a Vector4 where the RGB components represent the HDR color and the W component is the alpha value.
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 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.
nameIDusing 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))); }}
Additional Resources: MaterialPropertyBlock.SetFloat, MaterialPropertyBlock.SetVector, MaterialPropertyBlock.SetMatrix, MaterialPropertyBlock.SetTexture.
SetColor(int, Color)
Set a color property.
public void SetColor(int nameID, Color value)
Parameters
The name ID of the property retrieved by Shader.PropertyToID.
Remarks
Adds a property to the block. If a color property with the given name already exists, the old value is replaced.
The method assumes that the color value is in sRGB space. The method converts the value to linear color space if the active project color space is linear.
if you switch between color spaces in your project, Unity does not convert color values automatically. You need to handle the conversion yourself.
The method does not support setting HDR colors directly.
SetColorTo set HDR colors, use MaterialPropertyBlock.SetVector instead, passing a Vector4 where the RGB components represent the HDR color and the W component is the alpha value.
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 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.
nameIDusing 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))); }}