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 do you listen to a specific action globally?

Discussion in 'Input System' started by Armetron, May 2, 2020.

  1. Armetron

    Armetron

    Joined:
    Oct 7, 2016
    Posts:
    26
    Currently I have a local multiplayer project where I have 2+ player characters each with a player input component. I have a Canvas that contains my pause menu UI and logic. I need to have the canvas activate when either player activates a specific action (start). I HAVE managed to make it work by subscribing a function to the InputSystem onActionChange event and checking inputaction's name and state but it feels like such a roundabout way of doing it. I want to know if there is a better way

    Code (CSharp):
    1. private void OnEnable()
    2. {
    3.     InputSystem.onActionChange += TogglePauseMenu;
    4. }
    5.  
    6. private void OnDisable()
    7. {
    8.     InputSystem.onActionChange -= TogglePauseMenu;
    9. }
    10.  
    11. private void TogglePauseMenu(object action, InputActionChange change)
    12. {
    13.     if (change == InputActionChange.ActionPerformed)
    14.     {
    15.         if (((InputAction)action).name == "Start")
    16.         {
    17.             if (GameIsPaused)
    18.             {
    19.                 Resume();    //Turn off pause menu and resume game
    20.             }
    21.             else
    22.             {
    23.                 Pause();    //pause game and show menu
    24.             }
    25.         }
    26.     }
    27. }
     
    Last edited: May 2, 2020
  2. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    ATM, no. One can listen on individual actions, on entire InputActionMaps, or globally through onActionChange. There's no way currently to subscribe to an individual action globally.
     
  3. Armetron

    Armetron

    Joined:
    Oct 7, 2016
    Posts:
    26
    So the way I am doing it is the best way correct?