Version: Unity 6.3 LTS (6000.3)
Language : English
Masking sprites
Sprite Mask component reference

Add a sprite mask

To hide parts of sprites, add a spriteA 2D graphic objects. If you are used to working in 3D, Sprites are essentially just standard textures but there are special techniques for combining and managing sprite textures for efficiency and convenience during development. More info
See in Glossary
mask to the sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary
. You place a sprite maskA texture which defines which areas of an underlying image to reveal or hide. More info
See in Glossary
at a position in the scene, and the mask shape hides the sprites that overlap with it.

A circular mask, with a crate sprite set to Visible Inside Mask. The parts of the sprite outside the mask are hidden. The same circular mask, with a crate sprite set to Visible Outside Mask. The parts of the sprite that overlap the mask are hidden.

Sprite masks only affect objects that have a Sprite RendererA component that lets you display images as Sprites for use in both 2D and 3D scenes. More info
See in Glossary
component.

Note: Sprite masks are different to the mask map texture you add as a secondary texture to control which areas of a sprite receive light.

Limitations

Sprite masks aren’t compatible with the following:

  • Scriptable Render Pipeline (SRP) Batcher. If you use a sprite mask, Unity falls back to using dynamic batchingAn automatic Unity process which attempts to render multiple meshes as if they were a single mesh for optimized graphics performance. The technique transforms all of the GameObject vertices on the CPU and groups many similar vertices together. More info
    See in Glossary
    .
  • GPU deformation of sprites in 2D animation. If you use a sprite mask, Unity falls back to using CPU deformation.

Add a sprite mask

Follow these steps:

  1. In your 2D renderer asset, make sure Depth/Stencil BufferA memory store that holds an 8-bit per-pixel value. In Unity, you can use a stencil buffer to flag pixels, and then only render to pixels that pass the stencil operation. More info
    See in Glossary
    is enabled.

  2. To add a sprite mask to the scene, select GameObject > 2D Object > Sprite Mask.

    The default mask shape is a circle.

  3. In the Hierarchy window, select the sprite you want to mask.

  4. In the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
    See in Glossary
    window of the sprite, set Mask Interaction to Visible Inside Mask or Visible Outside Mask.

  5. Make sure the sprite overlaps with the sprite mask.

Change the shape of a mask

To change the shape of a mask, follow these steps:

  1. Select the sprite mask GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
    See in Glossary
    .
  2. In the Inspector window, select the Sprite picker ().
  3. Select a sprite from the list to use as a mask.

To use a custom shape, create a new sprite with the shape you want to use. Use opaque pixelsThe smallest unit in a computer image. Pixel size depends on your screen resolution. Pixel lighting is calculated at every screen pixel. More info
See in Glossary
for the mask shape, and a transparent background for outside the mask.

Set which sprites a mask affects

To change which sprites a mask affects, assign the sprites to sorting layers, then restrict the sprite mask to a range of sorting layers using its Custom Range properties.

For example:

  1. Open the Tags and Layers window and create three sorting layers: Back, Middle, and Front.
  2. Create three sprites, and set their Sorting Layer properties to the different layers.
  3. Select your sprite mask, and in the Inspector window enable Custom Range.
  4. Set the front Sorting Layer to Front, and the back Sorting Layer to Back. The sprite mask now only affects the sprites in the Front and Middle sorting layers.

You can also use Sorting Group components to prevent multiple masks from interacting with each other.

Left: Two sprite masks affect two card sprites, so the sprites overlap incorrectly. Right: Each sprite and its mask use their own sorting group, and the sprites overlap correctly.
Left: Two sprite masks affect two card sprites, so the sprites overlap incorrectly. Right: Each sprite and its mask use their own sorting group, and the sprites overlap correctly.

Additional resources

Masking sprites
Sprite Mask component reference