Created
January 27, 2017 22:31
-
-
Save flushpot1125/37fb4586fec8562e16b8b9bfa58496e4 to your computer and use it in GitHub Desktop.
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
/*copy from "https://developer.microsoft.com/en-us/windows/holographic/holograms_101"*/ | |
using UnityEngine; | |
using UnityEngine.VR.WSA.Input; | |
public class GazeGestureManager : MonoBehaviour | |
{ | |
public static GazeGestureManager Instance { get; private set; } | |
// Represents the hologram that is currently being gazed at. | |
public GameObject FocusedObject { get; private set; } | |
GestureRecognizer recognizer; | |
// Use this for initialization | |
void Awake() | |
{ | |
Instance = this; | |
// Set up a GestureRecognizer to detect Select gestures. | |
recognizer = new GestureRecognizer(); | |
recognizer.TappedEvent += (source, tapCount, ray) => | |
{ | |
// Send an OnSelect message to the focused object and its ancestors. | |
if (FocusedObject != null) | |
{ | |
FocusedObject.SendMessageUpwards("OnSelect"); | |
} | |
}; | |
recognizer.StartCapturingGestures(); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
// Figure out which hologram is focused this frame. | |
GameObject oldFocusObject = FocusedObject; | |
// Do a raycast into the world based on the user's | |
// head position and orientation. | |
var headPosition = Camera.main.transform.position; | |
var gazeDirection = Camera.main.transform.forward; | |
RaycastHit hitInfo; | |
if (Physics.Raycast(headPosition, gazeDirection, out hitInfo)) | |
{ | |
// If the raycast hit a hologram, use that as the focused object. | |
FocusedObject = hitInfo.collider.gameObject; | |
} | |
else | |
{ | |
// If the raycast did not hit a hologram, clear the focused object. | |
FocusedObject = null; | |
} | |
// If the focused object changed this frame, | |
// start detecting fresh gestures again. | |
if (FocusedObject != oldFocusObject) | |
{ | |
recognizer.CancelGestures(); | |
recognizer.StartCapturingGestures(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment