BlackDragonBE

Untitled

Jun 17th, 2016
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5.  
  6. // To use:
  7. // 1. Drag onto your EventSystem game object.
  8. // 2. Disable any other Input Modules (eg: StandaloneInputModule & TouchInputModule) as they will fight over selections.
  9. // 3. Make sure your Canvas is in world space and has a GraphicRaycaster (should by default).
  10. // 4. If you have multiple cameras then make sure to drag your VR (center eye) camera into the canvas.
  11. public class OcculusGazeInputModule : PointerInputModule {
  12.   public RaycastResult CurrentRaycast;
  13.  
  14.   private PointerEventData pointerEventData;
  15.   private GameObject currentLookAtHandler;
  16.  
  17.   public override void Process() {
  18.     HandleLook();
  19.     HandleSelection();
  20.   }
  21.  
  22.   void HandleLook() {
  23.     if (pointerEventData == null) {
  24.       pointerEventData = new PointerEventData(eventSystem);
  25.     }
  26.  
  27.     // fake a pointer always being at the center of the screen
  28.     pointerEventData.Reset();
  29.     pointerEventData.position = new Vector2(Screen.width / 2f, Screen.height / 2f);
  30.     pointerEventData.delta = Vector2.zero;
  31.     List<RaycastResult> raycastResults = new List<RaycastResult>();
  32.     eventSystem.RaycastAll(pointerEventData, raycastResults);
  33.     CurrentRaycast = pointerEventData.pointerCurrentRaycast = FindFirstRaycast(raycastResults);
  34.  
  35.     Debug.Log(CurrentRaycast);
  36.  
  37.     ProcessMove(pointerEventData);
  38.   }
  39.  
  40.   void HandleSelection() {
  41.     if (pointerEventData.pointerEnter != null) {
  42.       // if the ui receiver has changed, reset the gaze delay timer
  43.       GameObject handler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(pointerEventData.pointerEnter);
  44.       if (currentLookAtHandler != handler) {
  45.         currentLookAtHandler = handler;
  46.       }
  47.  
  48.       // if we have a handler and it's time to click, do it now
  49.       if (currentLookAtHandler != null && (OVRInput.Get(OVRInput.Button.Start) || OVRInput.Get(OVRInput.Button.One))) {
  50.         ExecuteEvents.ExecuteHierarchy(currentLookAtHandler, pointerEventData, ExecuteEvents.pointerClickHandler);
  51.       }
  52.     }
  53.     else {
  54.       currentLookAtHandler = null;
  55.     }
  56.   }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment