Bewerken

Delen via


MaterialInstance Class

Definition

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.

public ref class MaterialInstance : UnityEngine::MonoBehaviour
[UnityEngine.AddComponentMenu("Scripts/MRTK/Core/MaterialInstance")]
[UnityEngine.ExecuteAlways]
[UnityEngine.HelpURL("https://docs.microsoft.com/windows/mixed-reality/mrtk-unity/features/rendering/material-instance")]
[UnityEngine.RequireComponent(typeof(UnityEngine.Renderer))]
public class MaterialInstance : UnityEngine.MonoBehaviour
[<UnityEngine.AddComponentMenu("Scripts/MRTK/Core/MaterialInstance")>]
[<UnityEngine.ExecuteAlways>]
[<UnityEngine.HelpURL("https://docs.microsoft.com/windows/mixed-reality/mrtk-unity/features/rendering/material-instance")>]
[<UnityEngine.RequireComponent(typeof(UnityEngine.Renderer))>]
type MaterialInstance = class
    inherit MonoBehaviour
Public Class MaterialInstance
Inherits MonoBehaviour
Inheritance
UnityEngine.MonoBehaviour
MaterialInstance
Attributes
UnityEngine.AddComponentMenuAttribute UnityEngine.ExecuteAlwaysAttribute UnityEngine.HelpURLAttribute UnityEngine.RequireComponentAttribute

Constructors

MaterialInstance()

Properties

CacheSharedMaterialsFromRenderer

Whether to use a cached copy of cachedRenderer.sharedMaterials or call sharedMaterials on the Renderer directly. Enabling the option will lead to better performance but you must turn it off before modifying sharedMaterials of the Renderer.

Material

Returns the first instantiated Material assigned to the renderer, similar to Renderer.material.

Materials

Returns all the instantiated materials of this object, similar to Renderer.materials.

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.

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.

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).

Applies to