Search Unity

Gamepad Supported UI

Discussion in 'UI Toolkit' started by jmattarock_unity, Jan 6, 2020.

  1. jmattarock_unity

    jmattarock_unity

    Joined:
    Mar 10, 2018
    Posts:
    25
    Trying to get the gamepad to work in a UI like a mouse would. Can't seem to find much documentation out there on it. Anyone have any insight? looking to give the gamepad a cursor and having it be able to click on menu items and drag and drop. I assume IPointerClickHandler doesn't work for gamepad. Any direction or insight would be great.
     
  2. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
    Hi,

    The UIElements for runtime is highly experimental and under heavy development.
    Sadly gamepad support is something we haven't tackle yet.
     
  3. tststs

    tststs

    Joined:
    Jul 16, 2016
    Posts:
    11
    Any estimate for this topic?
     
  4. HrabiaWrzontq

    HrabiaWrzontq

    Joined:
    Nov 28, 2016
    Posts:
    16
    Bump

    I've been learning UIToolkit for a while now. We are developing a game for PC and consoles so if the pad support is not coming in a year or two, then I will have to simply drop using UIToolkit.
    We would like to hear any estimate.
     
  5. uBenoitA

    uBenoitA

    Unity Technologies

    Joined:
    Apr 15, 2020
    Posts:
    220
    Indeed, supporting the gamepad is on the list of important features we're very much committed to providing in UIToolkit, and more and more support will be coming as we progress with releases. As for an estimate on date of delivery, I can say that we already have some support for it in the current preview package, in particular if using UIToolkit's EventSystem alongside the Input System package. For example, gamepad-driven submit/cancel and navigation are fully supported in basic controls such as the Button, Toggle, and ListView. More core features are currently in development and should hopefully coincide with the release of Unity 2021.1.

    Some information on how to use the current version of UIToolkit's EventSystem with the Input System package can be found in this forum thread: https://forum.unity.com/threads/feedback-wanted-new-input-system-support.963111/

    As for your specific use case, simulating a cursor position and up/down states through the gamepad will currently require a bit more work on your part, although it should be feasible if your game needs to have its prototype going as early as possible. In the current preview of UIToolkit, what I can recommend is that you can use the VisualElement.SendEvent method with PointerDownEvent, PointerUpEvent, or PointerMoveEvent to effectively make UIToolkit elements react as though a pointer was pressed, released, or moved respectively. To dispatch that event to the entire panel hierarchy, you can send it to the VisualElement provided by your panel's "visualTree" property, and leave the event's "target" field empty. To instead send it to a specific element in particular, you need to set the event's "target" field to that element.

    We do have plans to make UIToolkit's EventSystem more flexible and more open to being modified by developers directly, so that simulating mouse events from the gamepad can happen in the EventSystem directly for example. We have some features regarding that planned in the current development cycle for our next package update, but the exact form it will take is still to be determined.

    Thank you for bringing back this interesting use case. This is exactly the kind of user feedback we need to keep in mind when test driving our package and making sure it covers all the required ground. I hope we'll be able to give you a more complete solution in the near future. If you do end up dropping UIToolkit (which would be unfortunate), we're very interested in hearing about the features you felt were missing and influenced your decision.

    Best of luck in your game development efforts.
     
    Last edited: Oct 11, 2020
  6. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    243
    Another bump !

    Do you have more news about UI Navigation ? (maybe for the next package update ? :p)

    I also would like to know if you will recommend to use (extend?) your navigation/focus system, or if you will encourage developers to make theirs. At least for basic gamepad navigation in menus, without pointer.

    I have been fiddling around the current navigation/focus(able) system provided by UI Toolkit (1.0.0-preview.13). Basically, I just flagged all buttons as Focusable (and I plugged the Input System package for testing). By the way, is it how we are supposed to enable navigation ?

    Navigation works pretty nice, out-of-the-box, but here are my main remarks :

    • Navigation "loop" works ! It's great, but developers will probably like to disable navigation loops (and maybe set "end of navigation" borders?)
    UI_Nav_Loop.gif
    • When going from one container (of focusable elements) to another, the next focused element isn't the most intuitive. During my tests, it looked like the next element was always the first neighbour according to the flex-direction of the target container (see the following gif). I am wondering if developers will be able to extend/override the focus switch behaviour (I glanced over FocusController.cs and there were a lot of internal methods).
    UI_Unsatisfying_Navigation.gif
    • Navigation still works with flex-wrap ! But same problem as previous point : the next focused element isn't the most intuitive.
    UI_WrapWorks_ButStillWeird.gif
    • As you said, ScrollViews are (still) unsupported (the ScrollView itself is "focusable" but the child VisualElements cannot get the focus). Could you give any visibility on when/if it will come ? (EDIT : it actually works sorry. Containers of focusable elements are not supposed to be focusable) Also, developers/designers will surely want to make their own animation for focus switching. But I guess we'll just have to register to the focus/navigation event and start the corresponding animation (scrolling + visual focus change).
     
    Last edited: Feb 4, 2021
  7. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    243
    I played a little more with the navigation system :

    Eventually, it would be very nice if we could change or give a custom navigation strategy to NavigateFocusRing.GetNextFocusable2D.
    What would be even nicer is if we could give our own implementation of IFocusRing to the Panels or FocusController(s) : is there a way to do that properly ? (or is it planned ? Or is it not possible by design ?)

    In the meanwhile, I made a custom VisualElement which prevents, depending on some bool attributes, the NavigationMoveEvent default action : hence the currently focused element won't try to switch focus.
    But it feels more like a workaround than a valid solution.

    upload_2021-2-3_19-32-46.png
     
  8. RichardJanssen

    RichardJanssen

    Joined:
    Apr 21, 2017
    Posts:
    14
    Hi, I’ve been working on trying to implement controller support for navigating the UI as well, but noticed that not much is possible or implemented yet. To make controllers work well with UGUI we had to override the evensystem and create our own buttons to make sure nothing goes wrong (such as buttons deselecting). Are there currently any plans on how Unity wishes to implement controller support for UI Toolkit in the future?, Here are a few controller support use case examples:
    • Navigating through the UI with a controller and simulating the press of a UI Button when you press on the “submit” button on your controller. Currently it is doable, but requires extra work as uBenoitA has stated. I’ve tried doing this myself in a test project and it isn’t very pretty currently.
    The code below shows the test I’ve made. I’m not sure if calling “Focus()” is necessary. The idea was to simulate the mouse hovering over the button when pressing “F” and the mouse clicking when pressing “G”.

    Code (CSharp):
    1. private void Update()
    2. {
    3.         if (Input.GetKeyDown(KeyCode.F))
    4.         {
    5.             pushSecondViewButton.Focus();
    6.             MouseEnterEvent mouseOver = MouseEnterEvent.GetPooled();
    7.             mouseOver.target = pushSecondViewButton;
    8.             pushSecondViewButton.SendEvent(mouseOver);
    9.         }
    10.  
    11.         if (Input.GetKeyDown(KeyCode.G))
    12.         {
    13.             MouseDownEvent mouseDown = MouseDownEvent.GetPooled();
    14.             mouseDown.target = pushSecondViewButton;
    15.             pushSecondViewButton.SendEvent(mouseDown);
    16.             NavigationSubmitEvent submit = NavigationSubmitEvent.GetPooled();
    17.             submit.target = pushSecondViewButton;
    18.             pushSecondViewButton.SendEvent(submit);
    19.         }
    20. }
    • “Pressing” buttons that cannot be navigated to using B/X/Y LB/RB controller buttons. Whilst it may be possible to bind manually through code, it’s really something you’d want to automate because almost every game needs this. In addition, you’d want to make sure the “press” effect would work to be passed through to the visual element to provide visual confirmation of players pressing the buttons.
     
  9. uBenoitA

    uBenoitA

    Unity Technologies

    Joined:
    Apr 15, 2020
    Posts:
    220
    All very good points. We've had some discussions last year on how to extend the runtime event system to make it easier to customize for games using other types of inputs than standard keyboard + mouse mappings. We discussed allowing users to manipulate buttons though more abstract "high-level" events which could be generated from "lower-level" events such as a GamePad button being pressed. Ultimately, we're very careful in our decisions when going forward in our development because once we enable a feature, we don't want to stop supporting it at any moment in the future if it turns out that it isn't the best way to provide the users with what they needed. We don't want to end up with a multitude of coexisting paradigms either (there are already a few too many). So, basically, we were waiting to get more user feedback and to better identify your needs. Thank you very much for sharing them!

    > Navigating through the UI with a controller and simulating the press of a UI Button
    Yes, sending mouse events manually is complicated and we never test for that use case when coding mouse events. I agree that we should include that as a proper case to support and add unit tests and a public code sample for that.

    > buttons can be deselected and that it breaks all controller navigation
    We are making things in UITK closer to UGUI right now, among other things we are maintaining that "deselect if nothing is clicked" behavior. I hear you, it should be optional. The main challenge right now is to think of the best place to put that option, but we probably need it, I agree. Making a note on that.

    > “Pressing” buttons that cannot be navigated to using B/X/Y LB/RB controller buttons
    Yes. Yes yes. Also going in the feature wish list for runtime event customization. I agree that there's not only a single way to press a button when you're in the mindset of using a GamePad, so the standard 1-to-1 mapping between input and events is not very well adapted. For starters, we could probably have a property in the submit event reflecting the input that caused it. Multiple inputs could then be mapped to all generate that event, and we could eventually provide a Button with a filter on the input that triggers it. Not sure yet what would be the simplest approach to this problem, but a similar mechanic is probably going to apply to pretty much all the other runtime-relevant events and controls, so we need to keep a pattern that will fit well on all of them at once.

    Thanks again for the suggestions, as I said we are listening closely to your feedback and accumulating wisdom to decide how we'll tackle the future of runtime events, so this is very invaluable to us!
     
    JTSuda, MousePods and RichardJanssen like this.
  10. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    811
    I just want to add my voice in saying that this is something I need also. Thanks for taking a look at it!

    Thanks @RichardJanssen for directing me me here!
     
    JTSuda and RichardJanssen like this.
  11. moatdd

    moatdd

    Joined:
    Jan 13, 2013
    Posts:
    178
    I'm also working with gamepad + UITK -- I've been trying to get ListViews to work with NavigationSubmitEvents but they don't seem to be receiving them even when they have the focus.
     
  12. k4sr4

    k4sr4

    Joined:
    Feb 26, 2018
    Posts:
    4
    Does UI Toolkit have an event system to support gamepad and keyboard now that it is added by default to the recent Unity versions?
     
  13. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    Yes. However, you need to set the :focus state in USS for any visual change to occur. In addition, you will also need to set the original focused element in code for each document you present.
     
    moatdd likes this.
  14. marcobethkeskyered

    marcobethkeskyered

    Joined:
    Nov 7, 2022
    Posts:
    6
    Hello, what is the current status of UI Toolkit with the new input system?

    I am having zero success making it work
     
    ElevenArt likes this.
  15. port80

    port80

    Joined:
    Aug 1, 2015
    Posts:
    1
    Bump. Gamepad support for UIToolkit is really critical and is not an edge-case.
     
    viknesh2020 and moatdd like this.