#if UNITY_INPUT_SYSTEM using System.IO; using System.Linq; using UnityEditor; using UnityEngine; using UnityEngine.InputSystem; namespace Unity.XR.XREAL.Editor { public static class XREALHandTrackingSetup { /// /// Configures the Input Action Asset for XREAL hand tracking by updating input bindings for both hands. /// /// /// This method allows users to set up hand tracking for XREAL devices by binding specific actions /// (such as position, rotation, selection, and UI interaction) to the corresponding inputs from XREAL's hand tracking system. /// It supports both "XRI Left/RightHand" and "XRI Left/Right" action maps, enabling compatibility with different configurations (e.g., XRI3). /// The updated Input Action Asset is saved back to its original file and re-imported into Unity. /// [MenuItem("XREAL/Setup Hand Tracking")] public static void SetupHandTracking() { if (Selection.activeObject is InputActionAsset actionAsset) { actionAsset.ModifyInputAction("XRI LeftHand", "Aim Position", "{LeftHand}/pointerPosition"); actionAsset.ModifyInputAction("XRI LeftHand", "Aim Rotation", "{LeftHand}/pointerRotation"); actionAsset.ModifyInputAction("XRI LeftHand Interaction", "Select", "{LeftHand}/indexPressed"); actionAsset.ModifyInputAction("XRI LeftHand Interaction", "Select Value", "{LeftHand}/pinchStrengthIndex"); actionAsset.ModifyInputAction("XRI LeftHand Interaction", "UI Press", "{LeftHand}/indexPressed"); actionAsset.ModifyInputAction("XRI LeftHand Interaction", "UI Press Value", "{LeftHand}/pinchStrengthIndex"); actionAsset.ModifyInputAction("XRI RightHand", "Aim Position", "{RightHand}/pointerPosition"); actionAsset.ModifyInputAction("XRI RightHand", "Aim Rotation", "{RightHand}/pointerRotation"); actionAsset.ModifyInputAction("XRI RightHand Interaction", "Select", "{RightHand}/indexPressed"); actionAsset.ModifyInputAction("XRI RightHand Interaction", "Select Value", "{RightHand}/pinchStrengthIndex"); actionAsset.ModifyInputAction("XRI RightHand Interaction", "UI Press", "{RightHand}/indexPressed"); actionAsset.ModifyInputAction("XRI RightHand Interaction", "UI Press Value", "{RightHand}/pinchStrengthIndex"); // XRI3 setup actionAsset.ModifyInputAction("XRI Left", "Aim Position", "{LeftHand}/pointerPosition"); actionAsset.ModifyInputAction("XRI Left", "Aim Rotation", "{LeftHand}/pointerRotation"); actionAsset.ModifyInputAction("XRI Left Interaction", "Select", "{LeftHand}/indexPressed"); actionAsset.ModifyInputAction("XRI Left Interaction", "Select Value", "{LeftHand}/pinchStrengthIndex"); actionAsset.ModifyInputAction("XRI Left Interaction", "UI Press", "{LeftHand}/indexPressed"); actionAsset.ModifyInputAction("XRI Left Interaction", "UI Press Value", "{LeftHand}/pinchStrengthIndex"); actionAsset.ModifyInputAction("XRI Right", "Aim Position", "{RightHand}/pointerPosition"); actionAsset.ModifyInputAction("XRI Right", "Aim Rotation", "{RightHand}/pointerRotation"); actionAsset.ModifyInputAction("XRI Right Interaction", "Select", "{RightHand}/indexPressed"); actionAsset.ModifyInputAction("XRI Right Interaction", "Select Value", "{RightHand}/pinchStrengthIndex"); actionAsset.ModifyInputAction("XRI Right Interaction", "UI Press", "{RightHand}/indexPressed"); actionAsset.ModifyInputAction("XRI Right Interaction", "UI Press Value", "{RightHand}/pinchStrengthIndex"); string assetPath = AssetDatabase.GetAssetPath(actionAsset); if (!string.IsNullOrEmpty(assetPath)) { File.WriteAllText(assetPath, actionAsset.ToJson()); AssetDatabase.ImportAsset(assetPath); } } else { Debug.LogError("Please select the current Input Action asset before setting up hand tracking."); } } static void ModifyInputAction(this InputActionAsset actionAsset, string actionMapName, string actionName, string binding) { var action = actionAsset.FindInputAction(actionMapName, actionName); if (action != null && !action.bindings.Any(b => b.path == binding)) { action.AddBinding(path: binding, groups: "Generic XR Controller"); } } } } #endif