- Home /
Sprite Mask that affects only the childrens of a GameObject
Hi everyone, I'm posting here because I want add a specific texture (A) on top of a sprite (B). To do this I modified a shader found on the internet, so that it takes the shape of my B sprite and shows only the parts of A that are inside B.
My problem is that if i have 2 sprites next to each other, their masks will afect the other sprite aswell. So I'm looking for a way to affect only 1 sprite with my mask, maybe on the childs of a same gameObject ?
I would really appreciate it if someone could help me on this, here is the code of my shader :
Shader "Custom/MaskTest" {
Properties
{
_MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
_Cutoff ("Base Alpha cutoff", Range (0,1)) = 0.5
}
SubShader {
Tags {"Queue" = "Transparent+1"}
Offset 0, -1
ColorMask 0
ZWrite On
Pass
{
AlphaTest Less [_Cutoff]
SetTexture [_MainTex] {
combine texture * primary, texture
}
}
}
}
Answer by IggyZuk · May 14, 2019 at 07:22 PM
Add Sorting Group component!
Sorting Group Component added to the Parent GameObject ensures the mask will affect only children of that Sorting Group
I would still be stuck, if it was not for the sorting group component!
Yes! Thank you. This solved my problem. Good to know they added this!
Sorry to necro, but is there a way to achieve the exact opposite effect? I want a mask that only affects other objects and not the children.
Basically I'm making small water particles that merge together when they're close.I'm trying to use a mask to achieve this, using a slightly bigger sprite as a child, so basically when two water droplets are close, i want the masks being made visible to in effect create a basic merging affect.
Answer by sam_nau · Aug 09, 2018 at 01:35 AM
Maybe I'm misunderstanding your problem but I think it can be solved very easily by using the new Sprite Mask component with a custom range. I had trouble understanding how to use the Sprite Mask on multiple objects at first. But the custom range option lets you isolate the masking of sprites to a sorting layer so that you can keep different masked sprites on different sorting layers and not have them interfere with each other. https://docs.unity3d.com/Manual/class-SpriteMask.html
It's not obvious right away that the custom range lets you have control over how the mask affects the sprites in your scene. Especially when you want multiple masks on different groups of sprites. But that's exactly what its for. You just pick a sort layer for the range after selecting that option and that's it.
I stumbled across this answer by mistake and it solved something that had been driving me crazy LOL
Answer by KazenoZ · Jul 04, 2017 at 09:03 AM
If I'm understanding the problem correctly, and if it suits your needs, you can use Unity's built in Alpha Mask UI component (https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-mask).
What you want to do with this is to add the Mask component on a child of the banana that has the banana sprite attached to it as well, and then child the blood splatter to that object.
The Mask component makes it so only pixels within the mask's sprite are displayed, while all others are omitted. This only affects children of the mask object.
Answer by LazyKnight · Oct 10, 2018 at 09:07 AM
Specific masks only affect the specific renderers.
The solution I just figured out here, ugly, but it works.
using UnityEngine;
namespace DCGame.UnityFeaturePatch
{
public class SpriteMaskRangeFixer : MonoBehaviour
{
private static int index = 0;
public SpriteRenderer[] renderers;
public SpriteMask[] masks;
public string sortingLayerName;
private void Start()
{
index += 2;
var sortingLayerID = SortingLayer.NameToID(sortingLayerName);
foreach (var renderer in renderers)
{
renderer.sortingOrder = index;
renderer.sortingLayerID = sortingLayerID;
}
foreach (var mask in masks)
{
mask.isCustomRangeActive = true;
mask.backSortingLayerID = sortingLayerID;
mask.backSortingOrder = index - 1;
mask.frontSortingOrder = index;
mask.frontSortingLayerID = sortingLayerID;
}
if (index >= 1000)
{
index = 0;
}
}
}
}
Related discussion: https://forum.unity.com/threads/sprite-mask-that-only-affects-child.538021/
Posted here as a note.
Your answer
Follow this Question
Related Questions
Issue with swapping Player sprite when health lowers - Not sure how to fix 1 Answer
Mask Interaction outside of a Sorting Group 0 Answers
How come setting sprite tint/alpha does not increase draw calls? 0 Answers
2d sprite Animation - Fire at frame 2 Answers
Sprite doesn't show on Canvas, can't change to Image because I need Sprite Renderer component 0 Answers