Search Unity

Input specific UI navigation (for example only for PAD1)

Discussion in 'UI Toolkit' started by kqmdjc8, Mar 16, 2021.

  1. kqmdjc8

    kqmdjc8

    Joined:
    Jan 3, 2019
    Posts:
    102
    Hello. I am creating a co-op game in which there are multiple input systems. Unity's buttons navigation seems nice but it does listen for inputs from all controllers. How do I specify it so that for example Button1 listens only for mouse clicks, Button2 listens only for PAD1? (there are more pads involved)
     
  2. uBenoitA

    uBenoitA

    Unity Technologies

    Joined:
    Apr 15, 2020
    Posts:
    220
    The bad news is, there is no easy integrated way to do what you're asking in UI Toolkit. The event system is centralized and all panels listen to only one common input source, as things are right now.

    However, the good news is, there should be a way for you to make this work by manually sending the right events to the appropriate elements, relying on some custom input update loop that you'd be managing on your side of things.

    Here's an example of how you could have only button1 receiving clicks and button2 receiving pad1 confirm button presses.

    Code (CSharp):
    1. ViualElement uiRoot;
    2. IPanel uiPanel;
    3.  
    4. void Start()
    5. {
    6.     uiRoot = GetComponent<UIDocument>().rootVisualElement;
    7.     uiPanel = uiRoot.panel;
    8.     uiRoot.RegisterCallback<PointerDownEvent>(OnPointerDown, TrickleDown.TrickleDown);
    9. }
    10.  
    11. void Update()
    12. {
    13.     // Replace this by code that checks that the focused element belongs to the pad1 player.
    14.     if (Input.GetButton("Pad1Confirm") && uiPanel.focusController.focusedElement?.name == "button2")
    15.     {
    16.         using (var evt = NavigationSubmitEvent.GetPooled())
    17.             uiRoot.SendEvent(evt);
    18.     }
    19. }
    20.  
    21. void OnPointerDown(PointerDownEvent evt)
    22. {
    23.     // Replace this by code that checks that the focused element belongs to the mouse player.
    24.     if (uiPanel.Pick(evt.position)?.name != "button1")
    25.         evt.StopPropagation();
    26. }
     
    Nexer8 likes this.
  3. kqmdjc8

    kqmdjc8

    Joined:
    Jan 3, 2019
    Posts:
    102
    Thanks for your reply @uBenoitA, but I have found a better way. I didn't know about MultiplayerEventSystem which handles this for us.