Class MaterialInstance
The MaterialInstance behavior aides in tracking instance material lifetime and automatically destroys instanced materials for the user. This utility component can be used as a replacement to Renderer.material or Renderer.materials. When invoking Unity's Renderer.material(s), Unity automatically instantiates new materials. It is the caller's responsibility to destroy the materials when a material is no longer needed or the game object is destroyed. The MaterialInstance behavior helps avoid material leaks and keeps material allocation paths consistent during edit and run time.
Namespace: Microsoft.MixedReality.Toolkit.Rendering
Assembly: cs.temp.dll.dll
Syntax
public class MaterialInstance : MonoBehaviour
Properties
Material
Returns the first instantiated Material assigned to the renderer, similar to Renderer.material.
Declaration
public Material Material { get; }
Property Value
Type | Description |
---|---|
Material |
Materials
Returns all the instantiated materials of this object, similar to Renderer.materials.
Declaration
public Material[] Materials { get; }
Property Value
Type | Description |
---|---|
Material[] |
Methods
AcquireMaterial(Object, Boolean)
Returns the first instantiated Material assigned to the renderer, similar to Renderer.material. If any owner is specified the instanced material(s) will not be released until all owners are released. When a material is no longer needed ReleaseMaterial should be called with the matching owner.
Declaration
public Material AcquireMaterial(object owner = null, bool instance = true)
Parameters
Type | Name | Description |
---|---|---|
Object | owner | An optional owner to track instance ownership. |
Boolean | instance |
Returns
Type | Description |
---|---|
Material | The first instantiated Material. |
AcquireMaterials(Object, Boolean)
Returns all the instantiated materials of this object, similar to Renderer.materials. If any owner is specified the instanced material(s) will not be released until all owners are released. When a material is no longer needed ReleaseMaterial should be called with the matching owner.
Declaration
public Material[] AcquireMaterials(object owner = null, bool instance = true)
Parameters
Type | Name | Description |
---|---|---|
Object | owner | An optional owner to track instance ownership. |
Boolean | instance | Should this acquisition attempt to instance materials? |
Returns
Type | Description |
---|---|
Material[] | All the instantiated materials. |
ReleaseMaterial(Object, Boolean)
Relinquishes ownership of a material instance. This should be called when a material is no longer needed after acquire ownership with AcquireMaterial(s).
Declaration
public void ReleaseMaterial(object owner, bool autoDestroy = true)
Parameters
Type | Name | Description |
---|---|---|
Object | owner | The same owner which originally acquire ownership via AcquireMaterial(s). |
Boolean | autoDestroy | When ownership count hits zero should the MaterialInstance component be destroyed? |