diff options
author | chai <chaifix@163.com> | 2021-05-08 23:15:13 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-05-08 23:15:13 +0800 |
commit | d07e14add74e017b52ab2371efeea1aa4ea10ced (patch) | |
tree | efd07869326e4c428f5bfe43fad0c2583d32a401 /Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask/MaskUtilities.cs |
+init
Diffstat (limited to 'Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask/MaskUtilities.cs')
-rw-r--r-- | Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask/MaskUtilities.cs | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask/MaskUtilities.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask/MaskUtilities.cs new file mode 100644 index 0000000..6abce5a --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask/MaskUtilities.cs @@ -0,0 +1,187 @@ +using System.Collections.Generic; +using UnityEngine.EventSystems; + +namespace UnityEngine.UI +{ + public class MaskUtilities + { + public static void Notify2DMaskStateChanged(Component mask) + { + var components = ListPool<Component>.Get(); + mask.GetComponentsInChildren(components); + for (var i = 0; i < components.Count; i++) + { + if (components[i] == null || components[i].gameObject == mask.gameObject) + continue; + + var toNotify = components[i] as IClippable; + if (toNotify != null) + toNotify.RecalculateClipping(); + } + ListPool<Component>.Release(components); + } + + public static void NotifyStencilStateChanged(Component mask) + { + var components = ListPool<Component>.Get(); + mask.GetComponentsInChildren(components); + for (var i = 0; i < components.Count; i++) + { + if (components[i] == null || components[i].gameObject == mask.gameObject) + continue; + + var toNotify = components[i] as IMaskable; + if (toNotify != null) + toNotify.RecalculateMasking(); + } + ListPool<Component>.Release(components); + } + + //从下往上找到第一个OverrideSorting的canvas或者root(即null) + public static Transform FindRootSortOverrideCanvas(Transform start) + { + var canvasList = ListPool<Canvas>.Get(); + start.GetComponentsInParent(false, canvasList); + Canvas canvas = null; + + for (int i = 0; i < canvasList.Count; ++i) + { + canvas = canvasList[i]; + + // We found the canvas we want to use break + if (canvas.overrideSorting) + break; + } + ListPool<Canvas>.Release(canvasList); + + return canvas != null ? canvas.transform : null; + } + + // 返回Mask在canvas下(canvas下所有的masks下)的深度 + public static int GetStencilDepth(Transform transform, Transform stopAfter) + { + var depth = 0; + if (transform == stopAfter) + return depth; + + var t = transform.parent; + var components = ListPool<Mask>.Get(); + while (t != null) + { + t.GetComponents<Mask>(components); + for (var i = 0; i < components.Count; ++i) + { + if (components[i] != null && components[i].MaskEnabled() && components[i].graphic.IsActive()) + { + ++depth; + break; + } + } + + if (t == stopAfter) + break; + + t = t.parent; + } + ListPool<Mask>.Release(components); + return depth; + } + + // father是child的祖先节点或者father == child + public static bool IsDescendantOrSelf(Transform father, Transform child) + { + if (father == null || child == null) + return false; + + if (father == child) + return true; + + while (child.parent != null) + { + if (child.parent == father) + return true; + + child = child.parent; + } + + return false; + } + + public static RectMask2D GetRectMaskForClippable(IClippable clippable) + { + List<RectMask2D> rectMaskComponents = ListPool<RectMask2D>.Get(); + List<Canvas> canvasComponents = ListPool<Canvas>.Get(); + RectMask2D componentToReturn = null; + + clippable.rectTransform.GetComponentsInParent(false, rectMaskComponents); + + if (rectMaskComponents.Count > 0) + { + for (int rmi = 0; rmi < rectMaskComponents.Count; rmi++) + { + componentToReturn = rectMaskComponents[rmi]; + if (componentToReturn.gameObject == clippable.gameObject) + { + componentToReturn = null; + continue; + } + if (!componentToReturn.isActiveAndEnabled) + { + componentToReturn = null; + continue; + } + clippable.rectTransform.GetComponentsInParent(false, canvasComponents); + for (int i = canvasComponents.Count - 1; i >= 0; i--) + { + if (!IsDescendantOrSelf(canvasComponents[i].transform, componentToReturn.transform) && canvasComponents[i].overrideSorting) + { + componentToReturn = null; + break; + } + } + return componentToReturn; + } + } + + ListPool<RectMask2D>.Release(rectMaskComponents); + ListPool<Canvas>.Release(canvasComponents); + + return componentToReturn; + } + + public static void GetRectMasksForClip(RectMask2D clipper, List<RectMask2D> masks) + { + masks.Clear(); + + List<Canvas> canvasComponents = ListPool<Canvas>.Get(); + List<RectMask2D> rectMaskComponents = ListPool<RectMask2D>.Get(); + clipper.transform.GetComponentsInParent(false, rectMaskComponents); + + if (rectMaskComponents.Count > 0) + { + clipper.transform.GetComponentsInParent(false, canvasComponents); + for (int i = rectMaskComponents.Count - 1; i >= 0; i--) + { + if (!rectMaskComponents[i].IsActive()) + continue; + bool shouldAdd = true; + for (int j = canvasComponents.Count - 1; j >= 0; j--) + { + // 如果rectMask2D是canvas的祖先节点且这个canvas勾选了OverrideSorting,那么这个rectMask2D就会失效 + if (!IsDescendantOrSelf(canvasComponents[j].transform, rectMaskComponents[i].transform) + && canvasComponents[j].overrideSorting) + { + shouldAdd = false; + break; + } + } + if (shouldAdd) + masks.Add(rectMaskComponents[i]); + } + } + + ListPool<RectMask2D>.Release(rectMaskComponents); + ListPool<Canvas>.Release(canvasComponents); + } + } +} |