Hi,
I’m trying to build an interaction system for VR. I’m trying to raycast from the camera directly forward to get the UI element in the middle of the view in VR. Rather than normal buttons I’m using images with the Event Trigger component and the Pointer Enter and Pointer Exit events starting and stopping the timer. My attempt to fake the pointer data is not working however and I’m not sure why.
GraphicRaycaster gr = GetComponent<GraphicRaycaster>(); ;
PointerEventData ped = new PointerEventData(null);
ped.position = new Vector2(Screen.width * .5f, Screen.height * .5f);
List<RaycastResult> results = new List<RaycastResult>();
gr.Raycast(ped, results);
Debug.DrawRay(transform.position, transform.forward * 10, Color.red);
Debug.Log(results.Count);
Am I even on the right track?
Any hints or points to other answers would be appreciated.
So I’ve solved it in a hacky sort of way.
PointerEventData pointerData = new PointerEventData(EventSystem.current);
pointerData.position = new Vector2(Screen.width * .5f, Screen.height * .5f);
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerData, results);
if(results.Count > 0)
{
EventTrigger newEvent = results[0].gameObject.GetComponent<EventTrigger>();
if (currentEvent == null)
{
Debug.Log(results[0].gameObject.name + " is under cursor");
currentEvent = newEvent;
currentEvent.OnPointerEnter(pointerData);
}
else if(newEvent != currentEvent && currentEvent != null)
{
currentEvent.OnPointerExit(pointerData);
currentEvent = null;
}
}
if(currentEvent != null && results.Count == 0)
{
currentEvent.OnPointerExit(pointerData);
currentEvent = null;
}
It’s not the most ideal way of calling the event, but apparently EventSystem.current.RaycastAll won’t trigger the OnPointerEnter/OnPointerExit events. Ideally I’d like to streamline this in the future so I’ll keep using the event trigger.