Skip to content

Instantly share code, notes, and snippets.

@WikkidEdd
Last active March 11, 2022 13:19
Show Gist options
  • Save WikkidEdd/83b6fdc374869c5d6ce33fe239b50071 to your computer and use it in GitHub Desktop.
Save WikkidEdd/83b6fdc374869c5d6ce33fe239b50071 to your computer and use it in GitHub Desktop.
Graphic Raycaster with a bound check. For use on world space canvases to improve performance of raycasts. Make sure all your UI elements on within the bounds of the canvas and are co-planar.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class GraphicRaycasterWithBoundsCheck : GraphicRaycaster
{
RectTransform _rectTransform;
RectTransform RectTransform
{
get
{
if(!_rectTransform)
{
_rectTransform = GetComponent<RectTransform>();
}
return _rectTransform;
}
}
public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
{
if (!RectTransformUtility.RectangleContainsScreenPoint(RectTransform, eventData.position, eventCamera))
return;
base.Raycast(eventData, resultAppendList);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment