Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug InputDevices.GetDevicesAtXRNode() returns the wrong device.

Discussion in 'VR' started by VirtualDestructor, Jun 18, 2023.

  1. VirtualDestructor

    VirtualDestructor

    Joined:
    May 3, 2014
    Posts:
    40
    I have some old code that I use for getting the name of the VR controllers being used so that I can properly set the offset of the hand models. It worked for a long time, but at some point it started returning the wrong device name. I am using a Valve Index, but InputDevices.GetDevicesAtXRNode() returns "Oculus Touch Controller OpenXR".

    The current device is the Valve Index, although I have the Oculus Software also installed on the PC. No Oculus device is connected.

    Everything else works (tracking, input, etc). Below is some code that reproduces the problem in my project.

    Am I using InputDevices.GetDevicesAtXRNode() incorrectly? Is there a better method to figure out what type of controller is being used?

    I am using Unity 2021.3.27

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5.  
    6. public class GetDeviceTest : MonoBehaviour {
    7.     [SerializeField] XRNode _node = XRNode.LeftHand;
    8.     [SerializeField] InputDeviceCharacteristics _requiredCharacteristics = InputDeviceCharacteristics.HeldInHand;
    9.  
    10.     IEnumerator Start() {
    11.         while ( !TryGetFirstValidDevice() ) {
    12.             yield return null;
    13.         }
    14.     }
    15.  
    16.     readonly List<InputDevice> _preallocatedDeviceList = new();
    17.     bool TryGetFirstValidDevice() {
    18.         _preallocatedDeviceList.Clear();
    19.         InputDevices.GetDevicesAtXRNode(_node, _preallocatedDeviceList);
    20.         foreach ( var device in _preallocatedDeviceList ) {
    21.             // I'm running into an issue where the struct has the isValid field set to true yet everything
    22.             // else about it is invalid, so check the characteristics to make sure the information in the struct
    23.             // is valid.
    24.             if ( (device.characteristics & _requiredCharacteristics) == _requiredCharacteristics && device.isValid ) {
    25.                 Debug.Log($"!!Using device {device.name}");
    26.                 Debug.Log($"!!Total devices found: {_preallocatedDeviceList.Count}");
    27.                 if ( _preallocatedDeviceList.Count > 1 ) {
    28.                     for ( int i = 0; i < _preallocatedDeviceList.Count; i++ ) {
    29.                         Debug.Log($"!!Device {i}: {_preallocatedDeviceList[i].name}");
    30.                     }
    31.                 }
    32.                 return true;
    33.             }
    34.         }
    35.      
    36.         return false;
    37.     }
    38. }
    Output:
    !!Using device Oculus Touch Controller OpenXR
    !!Total devices found: 1
     

    Attached Files:

  2. DarkSoulsBoss2

    DarkSoulsBoss2

    Unity Technologies

    Joined:
    Mar 24, 2023
    Posts:
    16