Last active
September 11, 2025 00:35
-
-
Save baba-s/8c012a0eb328c8d84670ec1ba633b939 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using JetBrains.Annotations; | |
| using UnityEngine; | |
| namespace Kogane | |
| { | |
| public static class UIOverlapChecker | |
| { | |
| private static readonly Vector3[] CORNERS = new Vector3[ 4 ]; | |
| public static bool IsOverlapping | |
| ( | |
| [NotNull] this RectTransform self, | |
| [NotNull] RectTransform rectTransform, | |
| [CanBeNull] Camera camera = null | |
| ) | |
| { | |
| if ( self == null ) return false; | |
| if ( rectTransform == null ) return false; | |
| var screenRect1 = GetScreenRect( self, camera ); | |
| var screenRect2 = GetScreenRect( rectTransform, camera ); | |
| return screenRect1.Overlaps( screenRect2 ); | |
| } | |
| private static Rect GetScreenRect | |
| ( | |
| [NotNull] RectTransform rectTransform, | |
| [CanBeNull] Camera camera | |
| ) | |
| { | |
| rectTransform.GetWorldCorners( CORNERS ); | |
| var bottomLeft = RectTransformUtility.WorldToScreenPoint( camera, CORNERS[ 0 ] ); | |
| var topRight = RectTransformUtility.WorldToScreenPoint( camera, CORNERS[ 2 ] ); | |
| var width = topRight.x - bottomLeft.x; | |
| var height = topRight.y - bottomLeft.y; | |
| return new Rect | |
| ( | |
| x: bottomLeft.x, | |
| y: bottomLeft.y, | |
| width: width, | |
| height: height | |
| ); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment