Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Resolved XR OnControllerConnect & OnControllerDisconnect

Discussion in 'VR' started by kay-developer, Aug 12, 2021.

  1. kay-developer

    kay-developer

    Joined:
    Oct 23, 2014
    Posts:
    5
    How can I determine if a XR controller is connected or not?

    I have tried two methods so far, without success.
    1. I tried to get this information from a instance of "XRBaseController", but I cant find a way to see if a controller is connected.
    2.Made use of InputDevices.GetDevicesAtXRNode. With this I can see when a controller is connected, but it doesn't seem to be able to tell me if a controller gets turned off.

    Code (CSharp):
    1. void Update()
    2. {
    3.         List<InputDevice> xrInputDevices = new List<InputDevice>();
    4.         InputDevices.GetDevicesAtXRNode(XRNode.RightHand, xrInputDevices);
    5.         bool isControllerConnected= false;
    6.         for (int i = 0; i < xrInputDevices.Count; ++i)
    7.             if (xrInputDevices[i].isValid)
    8.                 isControllerConnected= true;
    9.         this.isControllerConnected = isControllerConnected;
    10. }
     
  2. NevinAF

    NevinAF

    Joined:
    May 12, 2021
    Posts:
    36
    I believe this is exactly what you are looking for::

    XR Input under section Listening for device connections and disconnections:
    (h
    ttps://docs.unity3d.com/Manual/xr_input.html)

    "To avoid repeatedly checking if a device is connected to the platform, use InputDevices.deviceConnected and InputDevices.deviceDisconnected to notify your application when a device connects or disconnects."

    Then, all you should need is this:
    Code (CSharp):
    1. /* If you want to set "isControllerConnected" if the first device matches the XRNode:: */
    2. InputDevice xrInputDevice;
    3. bool isControllerConnected;
    4. IEnumerator Start()
    5. {
    6.      // Get target devices
    7.      List<InputDevice>() xrInputDevices = new List<InputDevice>();
    8.      InputDevices.GetDevicesAtXRNode(XRNode.RightHand, xrInputDevices);
    9.      while (xrInputDevices.Count <= 0)
    10.      {
    11.           // wait for a device to be found.
    12.           yield return null;
    13.           InputDevices.GetDevicesAtXRNode(XRNode.RightHand, xrInputDevices);
    14.      }
    15.      xrInputDevice = xrInputDevices[0];
    16.  
    17.      // Initialize if controller is connected
    18.      isControllerConnected = xrInputDevice.isValid;
    19.  
    20.      // Add listeners for devices being connected/disconected
    21.      InputDevices.deviceConnected += (device) =>
    22.      {
    23.           if (xrInputDevice == device)
    24.                connectedDevices++;
    25.      }
    26.      InputDevices.deviceDisconnected += (device) =>
    27.      {
    28.           if (xrInputDevice == device)
    29.                connectedDevices--;
    30.      }
    31. }
    32.  
    33. /* If you want to set "isControllerConnected" if ANY device matches the XRNode:: */
    34. List<InputDevice> xrInputDevices;
    35. int connectedDevices; // Keeps track of the current number
    36. bool isControllerConnected { get => connectedDevices > 0; } // returns true if at least one device is active.
    37. void Start()
    38. {
    39.      // Get target devices
    40.      xrInputDevices = new List<InputDevice>();
    41.      InputDevices.GetDevicesAtXRNode(XRNode.RightHand, xrInputDevices);
    42.  
    43.      // Initialize the number of connected devices
    44.      connectedDevices = 0;
    45.      foreach (InputDevice device in xrInputDevices)
    46.           if (device.isValid == true)
    47.                connectedDevices++;
    48.  
    49.      // Add listeners for devices being connected/disconected
    50.      InputDevices.deviceConnected += (device) =>
    51.      {
    52.           // xrInputDevices is fixed at the time of starting this component. Consider adding devices to the list when they are connected and match the XRNode.
    53.           if (xrInputDevices.Contains(device))
    54.                connectedDevices++;
    55.      }
    56.      InputDevices.deviceDisconnected += (device) =>
    57.      {
    58.           if (xrInputDevices.Contains(device))
    59.                connectedDevices--;
    60.      }
    61. }
     
    evolvingtech and kay-developer like this.
  3. kay-developer

    kay-developer

    Joined:
    Oct 23, 2014
    Posts:
    5
    This is exactly what I was looking for. Thank you!
     
    NevinAF likes this.