Unity - Scripting API: MaterialPropertyBlock
Version: Unity 6.5 (6000.5)
LanguageEnglish
  • C#

MaterialPropertyBlock

class in UnityEngine

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

A block of material values to apply.

MaterialPropertyBlock is used by Graphics.RenderMesh and Renderer.SetPropertyBlock. Use it in situations where you want to draw multiple objects with the same material, but slightly different properties. For example, if you want to slightly change the color of each mesh drawn. Changing the render state is not supported.

Unity's terrain engine uses MaterialPropertyBlock to draw trees; all of them use the same material, but each tree has different color, scale & wind factor.

The block passed to Graphics.RenderMesh or Renderer.SetPropertyBlock is copied, so the most efficient way of using it is to create one block and reuse it for all DrawMesh calls. Use SetFloat, SetVector, SetColor, SetMatrix, SetTexture, SetBuffer to add or replace values.

Note that this is not compatible with SRP Batcher. Using this in the Universal Render Pipeline (URP), High Definition Render Pipeline (HDRP) or a custom render pipeline based on the Scriptable Render Pipeline (SRP) will likely result in a drop in performance.

The following example creates 10 GameObjects with random colors using MaterialPropertyBlock. To use the example, attach a prefab to the myPrefab property.

Additional resources: Graphics.RenderMesh, Material.

using UnityEngine;

public class CreateTenGameObjects : MonoBehaviour { public GameObject myPrefab;

void Start() { // Loop through 10 GameObjects for (int i = 0; i < 10; i++) { // Instantiate a new GameObject at a unique position GameObject newObject = Instantiate(myPrefab, new Vector3(i * 2f, 0, 0), Quaternion.identity);

// Create a new MaterialPropertyBlock MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();

// Set a random color in the MaterialPropertyBlock propertyBlock.SetColor("_Color", Random.ColorHSV());

// Apply the MaterialPropertyBlock to the GameObject newObject.GetComponent<MeshRenderer>().SetPropertyBlock(propertyBlock); } } }

Properties

Public Methods