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

How many controllers connected

Discussion in 'Input System' started by PaRsOnIsPhErE, Jun 16, 2020.

  1. PaRsOnIsPhErE

    PaRsOnIsPhErE

    Joined:
    Dec 19, 2018
    Posts:
    19
    Hi all,

    Hope everyone is keeping well and busy during all this turmoil our planet is going through.

    I just want some tips, and too be nudged in the right direction for this as I feel like i'm going around in circles...

    All I want is to be able to find out how many controllers of a certain type are currently connected as an interger, so that certain levels are loaded based on the amount of players. Below is an example of what I expect it too look like. But cant find where in the new input system to get the numbers from... Gamepad.all provides an array, but I'd need an int only of a certain type. I am developing a game for the Switch so when Joycon support is added, for testing on PC, being able to get this number will be great.

    Code (CSharp):
    1. #if UNITY_EDITOR
    2.         loadedPlayers = XInputController.all;
    3.    #elif UNITY_PS4
    4.          loadedPlayers = DualShockGamepadPS4.all;
    5.     #elif UNITY_STANDALONE_WIN
    6.         loadedPlayers = XInputController.all;
    7.     #endif
    8.  
    9. //etc... etc...
    Thank you all for any help, advice.
     
  2. PaRsOnIsPhErE

    PaRsOnIsPhErE

    Joined:
    Dec 19, 2018
    Posts:
    19
    Actually I've decided I'm going to make a join screen on the main menu for the time being. Where each controller, connects by pressing a button, and this in turn adds to the loadedPlayers.
     
  3. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Just in case it's still helpful, number of ways you can go about it.

    Code (CSharp):
    1. // Using Enumerable extension API.
    2. InputSystem.device.OfType<XInputController>().Count();
    3.  
    4. // Manually iterating.
    5. foreach (var device in InputSystem.devices)
    6.     if (device is XInputController)
    7.         DebugLog("xxxxxboxxx");
    8.  
    9. // Using path matching API.
    10. var matches = InputSystem.FindControls("<XInputController>");
    11. matches.Dispose();
     
  4. PaRsOnIsPhErE

    PaRsOnIsPhErE

    Joined:
    Dec 19, 2018
    Posts:
    19
    Thanks for the reply Rene!

    Ahh right, I was playing around with manually iterating, the Enumerable extension though is such a clean way of doing it!

    For some reason I just didn't think of 'OfType'... thanks again, going to have another crack at this, much appreciated!
     
  5. RainerEngblom

    RainerEngblom

    Joined:
    Sep 10, 2020
    Posts:
    9
    I have an additional problem with this. My game is best suited for old school joysticks, but can also be played with game controllers. However, usb-joysticks are not detected by InputSystem, at least not in the Gamepad.all list, so I never know how many joysticks are present.
    Therefore I use the old input system for testing the game with joysticks and the new one with game controllers. I can't set an axis in the old system to detect all joysticks, because then the game controller interferes with it. If I set an axis to look for joystick1 or joystick2, it sometimes finds it, sometimes it's in he wrong usb port.
    Any idea how to see the number of usb joysticks in Unity?

    EDIT: I think I found a solution, loop through devices and check the type Joystick:
    Code (CSharp):
    1. foreach (InputDevice id in InputSystem.devices) {
    2.      if(id.GetType() == typeof(Joystick)) {
    3.           numberOfJoysticks++;
    4.      }
    5.  }
     
    Last edited: Mar 7, 2023
  6. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    103
    Code (CSharp):
    1. Joystick.all.Count