Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- // To use:
- // 1. Drag onto your EventSystem game object.
- // 2. Disable any other Input Modules (eg: StandaloneInputModule & TouchInputModule) as they will fight over selections.
- // 3. Make sure your Canvas is in world space and has a GraphicRaycaster (should by default).
- // 4. If you have multiple cameras then make sure to drag your VR (center eye) camera into the canvas.
- public class OcculusGazeInputModule : PointerInputModule {
- public RaycastResult CurrentRaycast;
- private PointerEventData pointerEventData;
- private GameObject currentLookAtHandler;
- public override void Process() {
- HandleLook();
- HandleSelection();
- }
- void HandleLook() {
- if (pointerEventData == null) {
- pointerEventData = new PointerEventData(eventSystem);
- }
- // fake a pointer always being at the center of the screen
- pointerEventData.Reset();
- pointerEventData.position = new Vector2(Screen.width / 2f, Screen.height / 2f);
- pointerEventData.delta = Vector2.zero;
- List<RaycastResult> raycastResults = new List<RaycastResult>();
- eventSystem.RaycastAll(pointerEventData, raycastResults);
- CurrentRaycast = pointerEventData.pointerCurrentRaycast = FindFirstRaycast(raycastResults);
- Debug.Log(CurrentRaycast);
- ProcessMove(pointerEventData);
- }
- void HandleSelection() {
- if (pointerEventData.pointerEnter != null) {
- // if the ui receiver has changed, reset the gaze delay timer
- GameObject handler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(pointerEventData.pointerEnter);
- if (currentLookAtHandler != handler) {
- currentLookAtHandler = handler;
- }
- // if we have a handler and it's time to click, do it now
- if (currentLookAtHandler != null && (OVRInput.Get(OVRInput.Button.Start) || OVRInput.Get(OVRInput.Button.One))) {
- ExecuteEvents.ExecuteHierarchy(currentLookAtHandler, pointerEventData, ExecuteEvents.pointerClickHandler);
- }
- }
- else {
- currentLookAtHandler = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment