Skip to content

Instantly share code, notes, and snippets.

@baba-s
Last active September 11, 2025 00:35
Show Gist options
  • Select an option

  • Save baba-s/8c012a0eb328c8d84670ec1ba633b939 to your computer and use it in GitHub Desktop.

Select an option

Save baba-s/8c012a0eb328c8d84670ec1ba633b939 to your computer and use it in GitHub Desktop.
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