privatebool m_shouldRecalculateClipRects = false; private Rect m_prevClipRect = default;
publicvoidPerformClipping() { // IDK why unity use ReferenceEquals, I just copied it from RectMask2D.cs :( if (ReferenceEquals(_canvas, null) || null == _clipTarget) { return; }
RectTransform selfRectTransform = transform as RectTransform; RectTransform targetTectTransform = _clipTarget.transform as RectTransform; if (RectTransformEx.IsNotIntersetedWithTargetRect(selfRectTransform, targetTectTransform)) { CancelClip(); m_prevClipRect = default; m_shouldRecalculateClipRects = false; return; }
Rect currentRect = selfRectTransform.rect; Vector2 selfPivot = selfRectTransform.pivot; // Get pivot position from clip rect if (RectTransformEx.TryCalculateLocalPositionInAnotherRect(selfRectTransform, targetTectTransform, null, out Vector2 tempLocalPos)) { currentRect.position = tempLocalPos; } else// Cant not calculate, maybe canvas is not valid { CancelClip(); m_prevClipRect = default; m_shouldRecalculateClipRects = false; return; }
// Rect's potion is at center point of the screen Vector2 delta = new Vector2(-currentRect.width * selfPivot.x, -currentRect.height * selfPivot.y); currentRect.position += delta;
// IDK if it is needed to clamp the clip rect Rect targetRect = targetTectTransform.rect; currentRect.xMin = Mathf.Clamp(currentRect.xMin, targetRect.xMin, targetRect.xMax); currentRect.xMax = Mathf.Clamp(currentRect.xMax, targetRect.xMin, targetRect.xMax); currentRect.yMin = Mathf.Clamp(currentRect.yMin, targetRect.yMin, targetRect.yMax); currentRect.yMax = Mathf.Clamp(currentRect.yMax, targetRect.yMin, targetRect.yMax);
publicboolIsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera) { if (_cutRects == null || _cutRects.Length == 0) { returnfalse; }
for (int i = 0, length = _cutRects.Length; i < length; i++) { if (RectTransformUtility.RectangleContainsScreenPoint(_cutRects[i], screenPoint, eventCamera)) { returnfalse; // Pass it } }