MaterialInstance Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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.
[UnityEngine.AddComponentMenu("Scripts/GraphicsTools/MaterialInstance")]
[UnityEngine.ExecuteAlways]
[UnityEngine.RequireComponent(typeof(UnityEngine.Renderer))]
public class MaterialInstance : UnityEngine.MonoBehaviour
[<UnityEngine.AddComponentMenu("Scripts/GraphicsTools/MaterialInstance")>]
[<UnityEngine.ExecuteAlways>]
[<UnityEngine.RequireComponent(typeof(UnityEngine.Renderer))>]
type MaterialInstance = class
inherit MonoBehaviour
Public Class MaterialInstance
Inherits MonoBehaviour
- Inheritance
-
UnityEngine.MonoBehaviourMaterialInstance
- Attributes
-
UnityEngine.AddComponentMenuAttribute UnityEngine.ExecuteAlwaysAttribute UnityEngine.RequireComponentAttribute
Constructors
MaterialInstance() |
Fields
InstancePostfix |
Default postfix applied to all instance material names. |
Properties
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. |
Instance(Material) |
Creates a new material instance based on a source material. The name is updated to reflect being an instance. |
IsInstance(Material) |
Returns true if a material is instanced (this is currently tracked by a material having the InstancePostfix in the name). |
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). |