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

Question DPad and Keyboard navigation not working with UI Toolkit

Discussion in 'Input System' started by brainwipe, Feb 10, 2023.

  1. brainwipe

    brainwipe

    Joined:
    Aug 21, 2017
    Posts:
    74
    I want to control navigation through the menu system using the input system and it's fine with mouse clicking but not keyboard or Dpad.
    • Player Input Debug is showing keyboard and DPad inputs correctly
    • UI bindings are default
    • Project -> Player -> Active Input Handling = Input System Package (New)
    • Player Input behaviour is Invoke C# Events
    • I use .clicked += for binding events in UI Toolkit
    When the start menu appears, the follow code is run:

    Code (CSharp):
    1.         private void OnEnable()
    2.         {
    3.             if (GameMachine.Instance == null)
    4.             {
    5.                 throw new InvalidOperationException("Menu screen needs Bootstrap to run");
    6.             }
    7.  
    8.             GameMachine.Instance.OnErrorChanged += ShowError;
    9.             InputManager.UI.Enable();
    10.             InputManager.Player.Disable();
    11.             InputManager.Periscope.Disable();
    12.             InputManager.DebugConsole.Disable();
    13.         }
    14.  
    15.         private void OnDisable()
    16.         {
    17.             GameMachine.Instance.OnErrorChanged -= ShowError;
    18.             InputManager.UI.Disable();
    19.         }
    I think it's because there is no focus set on the first item (Start) so the controller. I don't seem to be able to set focus and I think this is because the mouse take priority and the focus is lost.

    Can a menu screen be used by both mouse and keyboard? Do I have to trap the change in control scheme (playerInput.onControlsChanged) and then disable the mouse to let the DPad/keys work?

    Do I have to manually bind the movement of the focus and trap click events myself (I hope not).

    Setup image below.

    menu no controller.PNG

    Apologies if this should go in the UI Toolkit forum; as it's a cross over, I have no idea where the bug lies.

    Thanks in advance.
     
  2. shuntecoud

    shuntecoud

    Joined:
    Jan 23, 2020
    Posts:
    1
    This works for me:
    Code (CSharp):
    1. void Start()
    2. {
    3.     EventSystem.current.SetSelectedGameObject(GameObject.Find("PanelSettings"));
    4. }
    5.  
    6. void OnEnable()
    7. {
    8.     Button myButton = GetComponent<UIDocument>().rootVisualElement.Q<Button>("start-button");
    9.     myButton.clicked += () => print("button clicked");
    10.     myButton.Focus();
    11. }
    PanelSettings GameObject gets created as a child of InputSystemUIInputModule after entering play mode.
     
    brainwipe likes this.