Example of changing per-instance data at runtime in the Built-In Render Pipeline
An example of using MaterialPropertyBlock objects to set the color property of an instanced shader.
Read time 1 minuteLast updated 21 days ago
The following example demonstrates how to use MaterialPropertyBlock objects to set per-instance data for a group of GameObjects at runtime. It sets the property from the above shader examples to a random color.
_Colorusing UnityEngine;public class MaterialPropertyBlockExample : MonoBehaviour{ public GameObject[] objects; void Start() { MaterialPropertyBlock props = new MaterialPropertyBlock(); MeshRenderer renderer; foreach (GameObject obj in objects) { float r = Random.Range(0.0f, 1.0f); float g = Random.Range(0.0f, 1.0f); float b = Random.Range(0.0f, 1.0f); props.SetColor("_Color", new Color(r, g, b)); renderer = obj.GetComponent<MeshRenderer>(); renderer.SetPropertyBlock(props); } }}