- Home /
Make a sprite visible only on some objects
I have a shadow sprite, and I want it to be only visible on some objects, like an astreoid. How can I achieve this? (The shadow is a sprite, not done by lighting/shadowing)
Answer by Buckslice · Dec 22, 2017 at 12:57 AM
You can accomplish this with Sprite Masks. Add a SpriteMask component to your asteroids and assign the sprite to be the same one the SpriteRenderer is using. Then on your shadows SpriteRenderer set the Mask Interaction property to 'Visible Inside Mask'. The default SpriteMask options worked fine for me, but depending on the complexity of your scene / layer setup you may need to tweak them a bit.
Thank you. A mask will be used with any of the sprites in the scene. I thought it had to be child of the sprite. And I can do sorting with sorting group component or setting mask's custom range. Is there a way to make this more specific? Like, sorting the masks by tag, ins$$anonymous$$d of layer orders? So specific masks will work for specific sprites all through the scene?
I am not really sure, but I think you just need to use the custom sorting layer ranges on the masks for this to work. Tags not having an associated value with them won't work for sorting. You could probably make a script that automatically sets these ranges up for you somehow based on the mask tags. Still not really sure how that would work with only tags though. Like the ship shadow and asteroid are both tagged the same thing? You have to make sure the shadows are an order ahead in the layer then.
This is fine only if you have not many objects. Sprite $$anonymous$$asks breaks GPU instancing, which in turn ramps up the CPU cost if you have a lot of sprites needed to be masked. We need a better solution to go in pair with Instancing!
Your answer
Follow this Question
Related Questions
Prevent 2D mask to impact other gameobjects ? 3 Answers
Huge performance drop after using sprite Masks 2 Answers
2d "fake" shadow is running away 0 Answers
Mask Sprites 1 Answer
Dynamic sprite mask 1 Answer