Class RectMask2D
A 2D rectangular mask that allows for clipping / masking of areas outside the mask.
Inherited Members
Namespace: UnityEngine.UI
Assembly: UnityEngine.UI.dll
Syntax
[AddComponentMenu("UI (Canvas)/Rect Mask 2D", 14)]
[ExecuteAlways]
[DisallowMultipleComponent]
[RequireComponent(typeof(RectTransform))]
public class RectMask2D : UIBehaviour, IClipper, ICanvasRaycastFilter
Remarks
The RectMask2D behaves in a similar way to a standard Mask component. It differs though in some of the restrictions that it has. A RectMask2D: *Only works in the 2D plane *Requires elements on the mask to be coplanar. *Does not require stencil buffer / extra draw calls *Requires fewer draw calls *Culls elements that are outside the mask area.
Constructors
RectMask2D()
Protected default constructor. Use UnityEngine.GameObject.AddComponent<T>() to add a RectMask2D to a GameObject.
Declaration
protected RectMask2D()
Properties
canvasRect
Get the Rect for the mask in canvas space.
Declaration
public Rect canvasRect { get; }
Property Value
| Type | Description |
|---|---|
| Rect |
padding
Padding to be applied to the masking X = Left Y = Bottom Z = Right W = Top
Declaration
public Vector4 padding { get; set; }
Property Value
| Type | Description |
|---|---|
| Vector4 |
rectTransform
Helper function to get the RectTransform for the mask.
Declaration
public RectTransform rectTransform { get; }
Property Value
| Type | Description |
|---|---|
| RectTransform |
softness
The softness to apply to the horizontal and vertical axis.
Declaration
public Vector2Int softness { get; set; }
Property Value
| Type | Description |
|---|---|
| Vector2Int |
Methods
AddClippable(IClippable)
Add a IClippable to be tracked by the mask.
Declaration
public void AddClippable(IClippable clippable)
Parameters
| Type | Name | Description |
|---|---|---|
| IClippable | clippable | Add the clippable object for this mask |
IsRaycastLocationValid(Vector2, Camera)
Returns whether the given screen position is within this mask's rect.
Declaration
public virtual bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | sp | The screen position to test. |
| Camera | eventCamera | The camera for the raycast. |
Returns
| Type | Description |
|---|---|
| bool | True if the point is inside the masked rectangle, or if the component is disabled. |
OnCanvasHierarchyChanged()
Called when the state of the parent Canvas is changed.
Declaration
protected override void OnCanvasHierarchyChanged()
Overrides
Remarks
Sets flag to recalculate clip rects.
OnDestroy()
Called when destroyed. Unregisters from the ClipperRegistry.
Declaration
protected override void OnDestroy()
Overrides
OnDisable()
Called when it becomes disabled. Unregisters from the ClipperRegistry and notifies clippable children.
Declaration
protected override void OnDisable()
Overrides
OnEnable()
Called when it becomes enabled. Registers with the ClipperRegistry and notifies clippable children.
Declaration
protected override void OnEnable()
Overrides
OnTransformParentChanged()
Called when a direct or indirect parent has reparented to a new Transform. See MonoBehaviour.OnTransformParentChanged for more information.
Declaration
protected override void OnTransformParentChanged()
Overrides
Remarks
Sets flag to recalculate clip rects.
OnValidate()
Declaration
protected override void OnValidate()
Overrides
PerformClipping()
Applies the mask's clipping rectangle to all registered clippable children.
Declaration
public virtual void PerformClipping()
RemoveClippable(IClippable)
Remove an IClippable from being tracked by the mask.
Declaration
public void RemoveClippable(IClippable clippable)
Parameters
| Type | Name | Description |
|---|---|---|
| IClippable | clippable | Remove the clippable object from this mask |
UpdateClipSoftness()
Propagates the current clip softness to all registered clippable children.
Declaration
public virtual void UpdateClipSoftness()