summaryrefslogtreecommitdiff
path: root/Assets/Samples/XR Interaction Toolkit/3.1.2/Starter Assets/Scripts/GazeInputManager.cs
diff options
context:
space:
mode:
authorpryazha <pryadeiniv@mail.ru>2025-07-02 08:46:23 -0700
committerpryazha <pryadeiniv@mail.ru>2025-07-02 08:46:23 -0700
commit8263edd59284aba390aca011d25b79efecef4c48 (patch)
tree6346e2afaaabd32156601cafaf20d4ee813befaf /Assets/Samples/XR Interaction Toolkit/3.1.2/Starter Assets/Scripts/GazeInputManager.cs
Diffstat (limited to 'Assets/Samples/XR Interaction Toolkit/3.1.2/Starter Assets/Scripts/GazeInputManager.cs')
-rw-r--r--Assets/Samples/XR Interaction Toolkit/3.1.2/Starter Assets/Scripts/GazeInputManager.cs95
1 files changed, 95 insertions, 0 deletions
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
+{
+ /// <summary>
+ /// Manages input fallback for <see cref="XRGazeInteractor"/> when eye tracking is not available.
+ /// </summary>
+ 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;
+
+ /// <summary>
+ /// Enable fallback to head tracking if eye tracking is unavailable.
+ /// </summary>
+ public bool fallbackIfEyeTrackingUnavailable
+ {
+ get => m_FallbackIfEyeTrackingUnavailable;
+ set => m_FallbackIfEyeTrackingUnavailable = value;
+ }
+
+
+ bool m_EyeTrackingDeviceFound;
+
+ /// <summary>
+ /// See <see cref="MonoBehaviour"/>.
+ /// </summary>
+ protected void Awake()
+ {
+ // Check if we have eye tracking support
+ var inputDeviceList = new List<InputDevice>();
+ 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);
+ }
+
+ /// <summary>
+ /// See <see cref="MonoBehaviour"/>.
+ /// </summary>
+ 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);
+ }
+ }
+ }
+}