Last active
March 11, 2022 13:19
-
-
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.
This file contains 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 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