Unity Sprite Masks

On this page

    Introduction

    As a game developer, I use Unity's built-in components whenever possible. Built-in components make it easy to build my game. I don't need to start from scratch. One built-in component that is super powerful in 2D game development is Unity Sprite Masks.

    This comprehensive guide will delve into Unity Sprite Masks. By the end of this guide, you will understand what Sprite Masks are. You will know how to use Sprite Masks. You will also understand practical applications for Sprite Masks in 2D game development in Unity.

    What is a Sprite Mask?

    A Sprite Mask in Unity is a tool that allows you to hide or reveal specific parts of a sprite or a group of sprites. This feature is indispensable for crafting character cards and portraits. It is precious in creating detective, strategy, and puzzle games. With a Sprite Mask, you can achieve stunning effects and character card displays without additional art assets.

    Adding a Sprite to Your Scene

    To add a Sprite to your 2D Unity game, follow these steps:

    1. Navigate to the Menu Bar.
    2. Select “GameObject.”
    3. Go to “2D Object.”
    4. Go to “Sprites.”
    5. Choose “Square.”

    Adding a Sprite Mask to Your Scene

    To incorporate a Sprite Mask into your Unity 2D game, follow these simple steps:

    1. Navigate to the Menu Bar.
    2. Select "GameObject."
    3. Go to "2D Object."
    4. Choose "Sprite Mask."

    Masking a Sprite with Sprite Mask

    By default, sprites in Unity will ignore Sprite Masks. To change this behavior and enable Sprite Mask interaction, follow these steps:

    1. Select the Sprite you want to affect.
    2. Set the "Mask Interaction" option on your Sprite.
    3. Choose between "Visible Under Mask" or "Not Visible Under Mask."

    Masking Out a Circular Area

    If you wish to mask out a circular area, follow this straightforward process:

    1. Set the Sprite Mask's Sprite to a circular texture sprite.

    What Sprite Masks Apply To

    Sprite Masks only apply to sprite renderer components. You also need to set the Mask Interaction mode for the Sprite Renderer Component. You can choose "Visible Under Mask" or "Not Visible Under Mask."

    What is the difference between "Visible Under Mask" and "Not Visible Under Mask?"

    When set to Visible Under Mask, the Sprite will only appear when the mask overlaps it.

    When set to Not Visible Under Mask, the Sprite will only appear when the mask does not overlap.

    Can I make a Sprite Mask affect only certain Sprites?

    Yes. You can isolate a Sprite Mask to affect only specific sprites within your game. There are two options to achieve this:

    Option 1: Sorting Groups

    1. Add a sorting group to the parent game object.
    2. Place the Sprite Mask as a child of the sorting group.
    3. Add the sprites you want to mask as children of the Sprite Mask.
    4. The Sprite Mask will only impact the sprites within the sorting group.

    Option 2: Sorting Layer or Order in Layer

    1. Set your Sprite to a specific sorting layer and order in layers.
    2. In your sprite mask, configure the range to encompass these sprite properties.
    3. This approach allows you to control which sprites the Sprite Mask influences.

    Unity Sprite Mask vs. RectMask2D

    You need to feel comfortable distinguishing between Sprite Masks and RectMask2D in Unity.

    • Unity Sprite Mask: Used for in-game sprites. It makes it possible to use a shape to mask sprites or groups. It is suitable for visual effects and scene visibility.
    • RectMask2D: Used for UI elements. RectMask2D offers similar masking capabilities as a Sprite Mask. But Unity designed the RectMask2D for use in user interfaces. RectMask2D is like a UI Sprite Mask.

    FAQs

    What is a sprite mask?

    A sprite mask in Unity is a tool that allows developers to hide or reveal parts of a sprite or a group of sprites. It's handy for character cards, portraits, and various game genres.

    How do I add a sprite mask to my scene?

    To add a sprite mask, follow these steps: Menu Bar > GameObject > 2D Object > Sprite Mask.

    How do I mask a sprite with a sprite mask?

    To mask a sprite using a sprite mask, select the Sprite. Then, set the "Mask Interaction" option to either "Visible Under Mask" or "Not Visible Under Mask."

    What do sprite masks apply to?

    Sprite masks apply to objects with a sprite renderer component. The sprite renderer component must have a Mask Interaction mode of "Visible Under Mask" or "Not Visible Under Mask."

    Is it possible to make a Sprite Mask affect only a specific sprite?

    Yes. You can achieve this using either sorting groups or the sorting layers system. Learn more about Sorting Layers in our complete guide to Sorting Layers.

    Unity Sprite Mask vs. RectMask2D

    Developers use Unity Sprite Masks for in-game sprites. In contrast, you should use RectMask2D for UI elements. Each serves a distinct purpose in game development.

    Conclusion

    Unity Sprite Masks are your secret sauce to make fantastic 2D games. This tool makes it possible to change sprites during gameplay. Spend a few minutes thinking about the range of possibilities that this tool opens up. Then, try giving it a go in Unity yourself. 

    I have some resources if you want to learn more about Sprite Masks in Unity. 

    Join our newsletter to get the latest updates
    Sign Up
    Share
    Michael Sacco
    Founder & CEO

    Michael Sacco is the Founder and CEO of OccaSoftware where he specializes in developing game assets for Unity game developers. With a background ranging from startups to American Express, he's been building great products for more than 10 years.

    Tags
    2d
    art
    sorting layers
    Unity Basics
    unity
    sprites