Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Assign controller button to EventSystem Pressed function

Discussion in 'Input System' started by SkrobieDev, Dec 28, 2020.

  1. SkrobieDev

    SkrobieDev

    Joined:
    Nov 11, 2020
    Posts:
    2
    I am trying to use a usb Gamepad (NES controller clone) with Unity's Event System menu navigation. So far everything works with navigating the menu but I cannot actually click on any buttons. On any standard controller, buttonSouth will perform whatever action is on the button, but due to the NES controller's A and B buttons being mapped to "Button 2" and "Trigger", it doesn't press the button by default. I am wondering how to implement support for "Button 2 [usb gamepad]" to execute a button. I am using Unity's new Input System, and have an Input System UI Input Module attached to my Event System Object.

    Thanks in advance!
     
    kuralab likes this.
  2. SkrobieDev

    SkrobieDev

    Joined:
    Nov 11, 2020
    Posts:
    2
    I added a button in input Action that called this function:
    Code (CSharp):
    1.  
    2. void NSubmit() {
    3.      EventSystem.current.currentSelectedGameObject.GetComponent<Button>().onClick.Invoke();
    4.     }
    This threw null errors so i added a check

    Code (CSharp):
    1.  
    2. void NSubmit() {
    3. if (EventSystem.current.currentSelectedGameObject != null && EventSystem.current.currentSelectedGameObject.GetComponent<Button>() != null)
    4.      EventSystem.current.currentSelectedGameObject.GetComponent<Button>().onClick.Invoke();
    5.     }
    But then realized the reason I got a null error is because I had a dropdown menu in my menu, and call GetComponent<Button> is not universal with the dropdown menu's script.

    I'm not sure that I would want to hard code the functionality for the dropdown menu. I am looking for something better integrated into the EventSystem, as on
    Xbox Controller it works perfect with every GUI element
     
    Last edited: Dec 28, 2020