Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Detect joypad is connect or unplugged...

Discussion in 'macOS' started by lordubik, May 20, 2016.

  1. lordubik

    lordubik

    Joined:
    Feb 18, 2013
    Posts:
    71
    Hi, is it a way to know if a pad is connected or unplugged during game? For example, I play with the joypad and after few time unplugged the pad. Now, I would a message warning on screen that the pad is unplugged...

    is it possible?


    Thanks!
     
  2. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    4,346
    The only information you can get about attached joysticks in Unity is the Input.GetJoystickNames() string array of attached joysticks. You would have to check this array every 1-2 seconds on a timer to determine if anything has changed.
     
  3. lordubik

    lordubik

    Joined:
    Feb 18, 2013
    Posts:
    71
    Hi Guavaman, thanks for your answer, but Input.GetJoystickNames don't resolve my problem, because Unity cached Input.GetJoystickNames information, so, when I unplugged the joypad, I sill every the information inside the buffer.

    There is a way to force unity to refresh the Input.GetJoystickNames informations?

    thanks
     
  4. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    4,346
    No there isn't. That's the only piece of information Unity gives you about what joysticks are attached and you cannot force it to refresh if it isn't changing.

    GetJoystickNames is not cached. It will update when you remove the joystick, but you have to call the Input.GetJoystickNames again to get a new array because it is generated each time you call the function.

    GetJoystickNames has a lot of different issues many platforms Unity supports. For example, the array index of the joystick name does not align to the Unity joystick id on some platforms. On some platforms when a joystick is plugged in or removed, the array elements collapse changing all the joystick id's of all attached joysticks, while on others the array fills empty spaces with "". It's very inconsistent. In addition, hot-plugging does not work consistently across different platforms either.

    Because of these (and many more) problems with UnityEngine.Input, I decided to start from scratch and write Rewired, an advanced input system that gets input directly from the OS on many platforms so I could have functional, predictable, and reliable joystick input that would work for multi-player games with reliable hot-plugging. If you're interested in an input system that just works, please check out the free trial of Rewired.
     
    Last edited: May 21, 2016
  5. lordubik

    lordubik

    Joined:
    Feb 18, 2013
    Posts:
    71
    Oh, thanks for the informations :)
    May be that in Unity3d editor, Input.GetJoystickNames do not works because cached the Input.GetJoystickNames, is it possbile? I don't have try in release mode for now...
     
  6. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    4,346
    I have worked with Unity's input system on just about every platform it supports and it does not cache Input.GetJoystickNames on any platform, in the editor or in builds.

    OSX does have some major issues in the editor though. Controllers cannot be hot plugged in the editor after a certain Unity 5 version introduced a new bug. The joystick must be plugged in before the editor is launched in order for it to be recognized by Unity. This was not the case in older versions of Unity. This bug was reported to them many versions ago, but as far as I am aware it has not been fixed yet. This issue does not affect Rewired which implements its own native input solution on OSX.
     
    Last edited: May 21, 2016
  7. lordubik

    lordubik

    Joined:
    Feb 18, 2013
    Posts:
    71
    Thanks!
    however, I use build in "PC, Mac Standalone" in a iMac.
     
  8. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    4,346
    If the editor is running on OSX, the build target doesn't matter. OSX editor has a bug with joystick hot-plugging.
     
  9. lordubik

    lordubik

    Joined:
    Feb 18, 2013
    Posts:
    71
    ah ok, thanks :) now I am more relaxed :)
     
unityunity