Class Mask
A component for masking children elements.
Inherited Members
Namespace: UnityEngine.UI
Assembly: UnityEngine.UI.dll
Syntax
[AddComponentMenu("UI (Canvas)/Mask", 13)]
[ExecuteAlways]
[RequireComponent(typeof(RectTransform))]
[DisallowMultipleComponent]
public class Mask : UIBehaviour, ICanvasRaycastFilter, IMaterialModifier
Remarks
By using this element any children elements that have masking enabled will mask where a sibling Graphic would write 0 to the stencil buffer.
Constructors
Mask()
Protected default constructor. Use UnityEngine.GameObject.AddComponent<T>() to add a Mask to a GameObject.
Declaration
protected Mask()
Properties
graphic
The graphic associated with the Mask.
Declaration
public Graphic graphic { get; }
Property Value
| Type | Description |
|---|---|
| Graphic |
rectTransform
The RectTransform of this Mask's GameObject.
Declaration
public RectTransform rectTransform { get; }
Property Value
| Type | Description |
|---|---|
| RectTransform |
showMaskGraphic
Show the graphic that is associated with the Mask render area.
Declaration
public bool showMaskGraphic { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Methods
GetModifiedMaterial(Material)
Modifies the material for masking. Used when the mask needs to modify the stencil buffer.
Declaration
public virtual Material GetModifiedMaterial(Material baseMaterial)
Parameters
| Type | Name | Description |
|---|---|---|
| Material | baseMaterial | The base material to apply the masking modification to. |
Returns
| Type | Description |
|---|---|
| Material | The modified material for rendering. |
Remarks
The mask creates modified copies of the base Material that write to the stencil buffer so that child graphics can be clipped to the mask shape. Multiple nested masks use increasing stencil depths up to a maximum of 8. Override GetModifiedMaterial(Material) to customize behavior.
Examples
The graphic system typically calls this method when the mask needs a modified material. Override to apply custom stencil or shader logic. Call base first to get the default stencil material, then optionally apply custom properties (e.g. a different stencil comparison or color write mask).
public override Material GetModifiedMaterial(Material baseMaterial)
{
Material modified = base.GetModifiedMaterial(baseMaterial);
// apply custom stencil or shader logic, e.g. change comparison:
if (modified != null) modified.SetInt("_StencilComp", (int)CompareFunction.NotEqual);
return modified;
}
IsRaycastLocationValid(Vector2, Camera)
Returns whether the given screen position hits the visible (unmasked) area of this mask graphic.
Declaration
public virtual bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | sp | The screen position to test. |
| Camera | eventCamera | The camera used for the raycast. |
Returns
| Type | Description |
|---|---|
| bool | True if the position is within the unmasked area. |
MaskEnabled()
Returns whether this mask is enabled and its graphic is valid.
Declaration
public virtual bool MaskEnabled()
Returns
| Type | Description |
|---|---|
| bool | True if the mask is active and has a valid graphic. |
OnDisable()
Called when it becomes disabled. Notifies clippable children and triggers a material update.
Declaration
protected override void OnDisable()
Overrides
OnEnable()
Called when it becomes enabled. Notifies clippable children and triggers a material update.
Declaration
protected override void OnEnable()
Overrides
OnValidate()
Declaration
protected override void OnValidate()