Sprite Mask that only affects child

Hey everyone, I want to make a sprite mask that only applies to its children, since when used with multiple sprites and masks, the masks affects all other sprites and they can overlap each other.
I've tried checking off custom range under the sprite mask component but that function sometimes causes the sprite to be masked to glitch out and not appear? and also i have multiple layers with the order in layer so sometimes it would not work as intended anyways.
Anyone know any other methods that can cause a sprite mask to only affect a gameobject's child?
thanks

3 Likes

Also would really like this feature, using the sorting range doesn't seem to help when using sorting groups as it will mess around with the order of sprites. Trying to mask multiple elements that are next to one another has undesirable results, any workarounds to limit the mask to only specific/child objects would be greatly appreciated.

Gut the same issue, *bump

Same issue here, bump.

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;
      }
    }
  }
}
2 Likes

I had the same problem. That is my solution:

There is property isCustomRangeActive. If you put it to "true" the mask will affect objects only in specific sorting order from frontSortingOrder to backSortingOrder. So I just put my sprites (actually parents of sprite masks) in different sorting orders and attach fix script to every mask:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FixMask : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        SpriteMask mask = GetComponent<SpriteMask>();
        int so = transform.parent.gameObject.GetComponent<SpriteRenderer>().sortingOrder;
        mask.isCustomRangeActive = true;
        mask.backSortingOrder = so;
        mask.frontSortingOrder = so;
    }

    // Update is called once per frame
    void Update()
    {

    }
}
1 Like

To all future people: You can use a SortingGroup for this. Add it to the parent.

24 Likes

You legend! SortingGroups work perfectly :smile:

1 Like

I really want this feature too, it should mask the child objects

Thanks a lot carlosfritz !!

[quote=“carlosfritz”, post:7, topic: 706321]
To all future people: You can use a SortingGroup for this. Add it to the parent.
[/quote]
Legend, thank you!

Necroposting. Please use the "like" button to show appreciation.

Thread Closed.