From 8263edd59284aba390aca011d25b79efecef4c48 Mon Sep 17 00:00:00 2001 From: pryazha Date: Wed, 2 Jul 2025 08:46:23 -0700 Subject: init --- .../Starter Assets/Scripts/GazeInputManager.cs | 95 ++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Assets/Samples/XR Interaction Toolkit/3.1.2/Starter Assets/Scripts/GazeInputManager.cs (limited to 'Assets/Samples/XR Interaction Toolkit/3.1.2/Starter Assets/Scripts/GazeInputManager.cs') diff --git a/Assets/Samples/XR Interaction Toolkit/3.1.2/Starter Assets/Scripts/GazeInputManager.cs b/Assets/Samples/XR Interaction Toolkit/3.1.2/Starter Assets/Scripts/GazeInputManager.cs new file mode 100644 index 0000000..43195de --- /dev/null +++ b/Assets/Samples/XR Interaction Toolkit/3.1.2/Starter Assets/Scripts/GazeInputManager.cs @@ -0,0 +1,95 @@ +using System.Collections.Generic; +using UnityEngine.InputSystem; + +namespace UnityEngine.XR.Interaction.Toolkit.Samples.StarterAssets +{ + /// + /// Manages input fallback for when eye tracking is not available. + /// + public class GazeInputManager : MonoBehaviour + { + // This is the name of the layout that is registered by EyeGazeInteraction in the OpenXR Plugin package + const string k_EyeGazeLayoutName = "EyeGaze"; + + [SerializeField] + [Tooltip("Enable fallback to head tracking if eye tracking is unavailable.")] + bool m_FallbackIfEyeTrackingUnavailable = true; + + /// + /// Enable fallback to head tracking if eye tracking is unavailable. + /// + public bool fallbackIfEyeTrackingUnavailable + { + get => m_FallbackIfEyeTrackingUnavailable; + set => m_FallbackIfEyeTrackingUnavailable = value; + } + + + bool m_EyeTrackingDeviceFound; + + /// + /// See . + /// + protected void Awake() + { + // Check if we have eye tracking support + var inputDeviceList = new List(); + InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.EyeTracking, inputDeviceList); + if (inputDeviceList.Count > 0) + { + Debug.Log("Eye tracking device found!", this); + m_EyeTrackingDeviceFound = true; + return; + } + + foreach (var device in InputSystem.InputSystem.devices) + { + if (device.layout == k_EyeGazeLayoutName) + { + Debug.Log("Eye gaze device found!", this); + m_EyeTrackingDeviceFound = true; + return; + } + } + + Debug.LogWarning($"Could not find a device that supports eye tracking on Awake. {this} has subscribed to device connected events and will activate the GameObject when an eye tracking device is connected.", this); + + InputDevices.deviceConnected += OnDeviceConnected; + InputSystem.InputSystem.onDeviceChange += OnDeviceChange; + + gameObject.SetActive(m_FallbackIfEyeTrackingUnavailable); + } + + /// + /// See . + /// + protected void OnDestroy() + { + InputDevices.deviceConnected -= OnDeviceConnected; + InputSystem.InputSystem.onDeviceChange -= OnDeviceChange; + } + + void OnDeviceConnected(InputDevice inputDevice) + { + if (m_EyeTrackingDeviceFound || !inputDevice.characteristics.HasFlag(InputDeviceCharacteristics.EyeTracking)) + return; + + Debug.Log("Eye tracking device found!", this); + m_EyeTrackingDeviceFound = true; + gameObject.SetActive(true); + } + + void OnDeviceChange(InputSystem.InputDevice device, InputDeviceChange change) + { + if (m_EyeTrackingDeviceFound || change != InputDeviceChange.Added) + return; + + if (device.layout == k_EyeGazeLayoutName) + { + Debug.Log("Eye gaze device found!", this); + m_EyeTrackingDeviceFound = true; + gameObject.SetActive(true); + } + } + } +} -- cgit v1.2.3-70-g09d2