Search Unity

Sprite Mask that only affects child

Discussion in '2D' started by yobeetchaae, Jun 27, 2018.

  1. yobeetchaae

    yobeetchaae

    Joined:
    Jun 11, 2018
    Posts:
    5
    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
     
    shoopi, Milenica and MoonJellyGames like this.
  2. OtakuD

    OtakuD

    Joined:
    Apr 24, 2014
    Posts:
    49
    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.
     
  3. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    Gut the same issue, *bump
     
  4. LazyKnight

    LazyKnight

    Joined:
    Jul 28, 2017
    Posts:
    7
    Same issue here, bump.
     
  5. LazyKnight

    LazyKnight

    Joined:
    Jul 28, 2017
    Posts:
    7
    Specific masks only affect the specific renderers.
    The solution I just figured out here, ugly, but it works.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. namespace DCGame.UnityFeaturePatch
    5. {
    6.   public class SpriteMaskRangeFixer : MonoBehaviour
    7.   {
    8.     private static int index = 0;
    9.     public SpriteRenderer[] renderers;
    10.     public SpriteMask[] masks;
    11.     public string sortingLayerName;
    12.  
    13.     private void Start()
    14.     {
    15.       index += 2;
    16.  
    17.       var sortingLayerID = SortingLayer.NameToID(sortingLayerName);
    18.  
    19.       foreach (var renderer in renderers)
    20.       {
    21.         renderer.sortingOrder = index;
    22.         renderer.sortingLayerID = sortingLayerID;
    23.       }
    24.       foreach (var mask in masks)
    25.       {
    26.         mask.isCustomRangeActive = true;
    27.         mask.backSortingLayerID = sortingLayerID;
    28.         mask.backSortingOrder = index - 1;
    29.         mask.frontSortingOrder = index;
    30.         mask.frontSortingLayerID = sortingLayerID;
    31.       }
    32.  
    33.       if (index >= 1000)
    34.       {
    35.         index = 0;
    36.       }
    37.     }
    38.   }
    39. }
    40.  
     
    Last edited: Oct 10, 2018
    cakyol and vedram like this.
  6. timzes

    timzes

    Joined:
    May 10, 2020
    Posts:
    1
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FixMask : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         SpriteMask mask = GetComponent<SpriteMask>();
    11.         int so = transform.parent.gameObject.GetComponent<SpriteRenderer>().sortingOrder;
    12.         mask.isCustomRangeActive = true;
    13.         mask.backSortingOrder = so;
    14.         mask.frontSortingOrder = so;
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.        
    21.     }
    22. }
     
    EDevJogos likes this.
  7. carlosfritz

    carlosfritz

    Joined:
    Feb 17, 2018
    Posts:
    32
    To all future people: You can use a SortingGroup for this. Add it to the parent.
     
  8. Beauke

    Beauke

    Joined:
    Apr 2, 2018
    Posts:
    1
    You legend! SortingGroups work perfectly :D
     
    ecumedesjours likes this.
  9. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    781
    I really want this feature too, it should mask the child objects
     
  10. YannigSmagghe

    YannigSmagghe

    Joined:
    Nov 14, 2018
    Posts:
    6