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. Dismiss Notice

Question How do I...in script, detect if a controller is plugged in Runtime.

Discussion in 'Input System' started by Fressno, Sep 21, 2021.

  1. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    As the title says, im trying to find a way to detect when player 2 plugs in the second controller, in script.
    As i would like to start functions and UI effects when player 2 gets involved into the game.
    I know about this page: https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/HowDoI.html
    but i dont know how to implement it into a script, or where the script should be or why.

    I need a helping snippet example like most of the examples in the unity documents to fully understand how it works.
    Thanx
     
  2. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    324
    I reformatted Unity's code snippet into a more complete example:
    Code (CSharp):
    1. public class Example : MonoBehaviour
    2. {
    3.     void Awake()
    4.     {
    5.         InputSystem.onDeviceChange += OnDeviceChange;
    6.     }
    7.  
    8.     void OnDestroy()
    9.     {
    10.         InputSystem.onDeviceChange -= OnDeviceChange;
    11.     }
    12.  
    13.     public void OnDeviceChange(InputDevice device, InputDeviceChange change)
    14.     {
    15.         switch (change)
    16.         {
    17.             case InputDeviceChange.Added:
    18.                 // New Device.
    19.                 break;
    20.             case InputDeviceChange.Disconnected:
    21.                 // Device got unplugged.
    22.                 break;
    23.             case InputDeviceChange.Reconnected:
    24.                 // Plugged back in.
    25.                 break;
    26.             case InputDeviceChange.Removed:
    27.                 // Remove from Input System entirely; by default, Devices stay in the system once discovered.
    28.                 break;
    29.             default:
    30.                 // See InputDeviceChange reference for other event types.
    31.                 break;
    32.         }
    33.     }
    34. }
    The new snippet showcases the entire 'listening' process without causing a memory leak. (See C# Actions for more info on that).

    In
    OnDeviceChange()
    you can put your own code.
    In your case, you are probably looking to display UI elements for whenever Player 2 is
    Added
    or
    Reconnected
    .


    As for this:
    This particular script can be attached to any GameObject.
    Whenever an InputDevice undergoes a change,
    OnDeviceChange()
    will be performed no matter which GameObject this component is attached to.
    Unity's
    InputSystem.onDeviceChange
    is an Action. When you do
    +=
    or
    -=
    to an Action, you are adding or removing a subscriber.
    Whenever Unity invokes the
    InputSystem.onDeviceChange
    Action, all subscribers (I.E. your code) will be executed.
     
    Last edited: Sep 23, 2021
  3. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Amazing! thank you. you probably helped more people than me right now.
    Thanx yet again.
     
    NotaNaN likes this.
  4. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    324
    I'm glad I could be of assistance! :D
    Happy coding!
     
    Fressno likes this.