Best Way to Detect Raycast Hits on UI in Unity XR?

I’m working on a Unity XR project and I’m looking for the most efficient way to detect when a raycast is hitting a UI element so that I can trigger certain events, such as vibrating the controller or playing a sound. Here are the methods I’ve tried so far, but I feel they might not be the most efficient:

  1. Adding Hover Check Components to Every UI Element: Utilizing OnPointerEnter(PointerEventData eventData) for each UI component.
  2. Continuous Check in Update() Method: Using rayInteractor.TryGetCurrentUIRaycastResult(out result) to constantly check for hits.

Ideally, I’d like to add a single component to the GameObject with the Ray Interactor to make it “smart” enough to detect hits on UI elements, similar to the second method. However, I want to avoid continuously calling this hit check every frame in the Update() method as it seems inefficient.

Is there a possibility to fire events when hovering over UI elements with the Raycast Interactor, similar to how events are triggered for XR Interactables? Why does this not work for UIs, and is there a recommended approach to achieve this?

Any suggestions or insights would be greatly appreciated!


There are 2 events that you can bind to in the XR Ray Interactor and Near-Far Interactor called OnUIHoverEntered and OnUIHoverExited.

These should let you do what you want. These UI Hover events are apparently available in version 2.4 and later of the XR Toolkit .