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
         }
     }
 }

}

Add Sorting Group component!

Sprite Masks

Sorting Group Component added to the
Parent GameObject ensures the mask
will affect only children of that
Sorting Group

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.

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.

Got same problem, anyone?

If I’m understanding the problem correctly, and if it suits your needs, you can use Unity’s built in Alpha Mask UI component (UI Components - Unity Learn).

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.

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.