Вот исходники моего решения (два класса, один - хелпер)
UnityHelper.Current.OrderMinValue=-32767
Решение так себе т.к. сильно ресурсозатратно.
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public static class UnityExtensions
{
public static void SetColorAlpha(this Image image, float value)
{
image.color = new Color(image.color.r, image.color.g, image.color.b, value);
}
public static void SetColorAlpha(this SpriteRenderer spriteRenderer, float value)
{
spriteRenderer.color = new Color(spriteRenderer.color.r, spriteRenderer.color.g, spriteRenderer.color.b, value);
}
public static IEnumerable<Transform> GetAllChildrenRecurcive(this Transform transform)
{
foreach (Transform child in transform)
{
yield return child;
foreach (var innerChild in child.GetAllChildrenRecurcive())
{
yield return innerChild;
}
}
}
public static IEnumerable<T> GetComponents<T>(this IEnumerable<Transform> transforms)
{
return transforms.Select(t => t.GetComponent<T>()).Where(c => c != null);
}
}
public class MaskOpacityBehaviour : MonoBehaviour
{
private List<ChildSpriteMask> ChildSpriteMasks { get; set; }
private void Start()
{
ChildSpriteMasks = new();
var children = transform.GetAllChildrenRecurcive().GetComponents<SpriteRenderer>().ToList();
foreach (var child in children)
{
child.maskInteraction = SpriteMaskInteraction.VisibleOutsideMask;
var maskGo = new GameObject(child.name + "mask");
maskGo.transform.parent = transform;
var spriteMask = maskGo.AddComponent<SpriteMask>();
spriteMask.isCustomRangeActive = true;
UpdateSpriteMask(child, spriteMask);
ChildSpriteMasks.Add(new ChildSpriteMask(child, spriteMask));
}
}
private void Update()
{
foreach (var cm in ChildSpriteMasks)
{
UpdateSpriteMask(cm.Child, cm.SpriteMask);
}
}
private static void UpdateSpriteMask(SpriteRenderer child, SpriteMask spriteMask)
{
spriteMask.sprite = child.sprite;
spriteMask.frontSortingLayerID = child.sortingLayerID;
spriteMask.frontSortingOrder = child.sortingOrder - 1;
spriteMask.backSortingLayerID = child.sortingLayerID;
spriteMask.backSortingOrder = UnityHelper.Current.OrderMinValue;
spriteMask.transform.position = child.transform.position;
spriteMask.transform.rotation = child.transform.rotation;
spriteMask.transform.localScale = child.transform.localScale;
}
private class ChildSpriteMask
{
public ChildSpriteMask(SpriteRenderer child, SpriteMask spriteMask)
{
Child = child;
SpriteMask = spriteMask;
}
public SpriteRenderer Child { get; }
public SpriteMask SpriteMask { get; }
}
}