Version: Unity 6.1 Alpha (6000.1)
LanguageEnglish
  • C#

MaterialPropertyBlock

class in UnityEngine

/

Implemented in:UnityEngine.CoreModule

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

isEmptyIs the material property block empty? (Read Only)

Public Methods

ClearClear material property values.
CopyProbeOcclusionArrayFromThis function copies the entire source array into a Vector4 property array named unity_ProbesOcclusion for use with instanced Shadowmask rendering.
CopySHCoefficientArraysFromThis function converts and copies the entire source array into 7 Vector4 property arrays named unity_SHAr, unity_SHAg, unity_SHAb, unity_SHBr, unity_SHBg, unity_SHBb and unity_SHC for use with instanced light probe rendering.
GetColorGet a color from the property block.
GetFloatGet a float from the property block.
GetFloatArrayGet a float array from the property block.
GetIntThis method is deprecated. Use GetFloat or GetInteger instead.
GetIntegerGet an integer from the property block.
GetMatrixGet a matrix from the property block.
GetMatrixArrayGet a matrix array from the property block.
GetTextureGet a texture from the property block.
GetVectorGet a vector from the property block.
GetVectorArrayGet a vector array from the property block.
HasBufferChecks if MaterialPropertyBlock has the ComputeBuffer property with the given name or name ID. To set the property, use SetBuffer.
HasColorChecks if MaterialPropertyBlock has the Color property with the given name or name ID. To set the property, use SetColor.
HasConstantBufferChecks if MaterialPropertyBlock has the ConstantBuffer property with the given name or name ID. To set the property, use SetConstantBuffer.
HasFloatChecks if MaterialPropertyBlock has the Float property with the given name or name ID. To set the property, use SetFloat.
HasIntThis method is deprecated. Use HasFloat or HasInteger instead.
HasIntegerChecks if MaterialPropertyBlock has the Integer property with the given name or name ID. To set the property, use SetInteger.
HasMatrixChecks if MaterialPropertyBlock has the Matrix property with the given name or name ID. This also works with the Matrix Array property. To set the property, use SetMatrix.
HasPropertyChecks if MaterialPropertyBlock has the property with the given name or name ID. To set the property, use one of the Set methods for MaterialPropertyBlock.
HasTextureChecks if MaterialPropertyBlock has the Texture property with the given name or name ID. To set the property, use SetTexture.
HasVectorChecks if MaterialPropertyBlock has the Vector property with the given name or name ID. This also works with the Vector Array property. To set the property, use SetVector.
SetBufferSet a buffer property.
SetColorSet a color property.
SetConstantBufferSets a ComputeBuffer or GraphicsBuffer as a named constant buffer for the MaterialPropertyBlock.
SetFloatSet a float property.
SetFloatArraySet a float array property.
SetIntThis method is deprecated. Use SetFloat or SetInteger instead.
SetIntegerAdds a property to the block. If an integer property with the given name already exists, the old value is replaced.
SetMatrixSet a matrix property.
SetMatrixArraySet a matrix array property.
SetTextureSet a texture property.
SetVectorSet a vector property.
SetVectorArraySet a vector array property.