Created
February 2, 2017 09:38
-
-
Save dykarohora/519645e35656c8fa3d3f46bc1d2dd46c to your computer and use it in GitHub Desktop.
Use event system instead of message system.
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
using UnityEngine; | |
using UnityEngine.EventSystems; | |
using UnityEngine.VR.WSA.Input; | |
public class GazeGestureManager : MonoBehaviour { | |
public static GazeGestureManager Instance { get; private set; } | |
public GameObject FocusedObject { get; private set; } | |
GestureRecognizer recognizer; | |
private void Awake() { | |
Instance = this; | |
recognizer = new GestureRecognizer(); | |
recognizer.TappedEvent += (source, tapCount, ray) => { | |
if (FocusedObject != null) { | |
//FocusedObject.SendMessageUpwards("OnSelect"); | |
ExecuteEvents.Execute<ISelectable>( | |
target: FocusedObject, | |
eventData: null, | |
functor: (target, data) => { | |
target.OnSelect(); | |
}); | |
} | |
}; | |
recognizer.StartCapturingGestures(); | |
} | |
// Update is called once per frame | |
void Update () { | |
GameObject oldFocusObject = FocusedObject; | |
var headPosition = Camera.main.transform.position; | |
var gazeDirection = Camera.main.transform.forward; | |
RaycastHit hitInfo; | |
if(Physics.Raycast(headPosition, gazeDirection, out hitInfo)) { | |
FocusedObject = hitInfo.collider.gameObject; | |
} else { | |
FocusedObject = null; | |
} | |
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