Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Using new Input System with UI Toolkit

Discussion in 'Input System' started by johaga, Dec 22, 2020.

  1. johaga

    johaga

    Joined:
    Oct 10, 2019
    Posts:
    9
    I have a very simple scenario - I have an on screen button that is over some aspect of the in game world - i.e. the ground.

    I have a character that I can direct to move to anywhere on the ground, or I can click on the on screen button. Just imagine like any tactical game you've ever seen (randomly pulled from google)



    Somewhere between UI and Input System, I need to be able to click a button and have it NOT click the ground underneath it.

    Now, over in the UI threads, they've got some crazy thing about making a giant screen which intercepts clicks? But of course, that doesn't really work with the Input System.

    So I've no idea who's in charge of what, but it seems like this is actually an Input thing. I want an Action Map that is UI specific, and when it's used by the UITK Event System, it doesn't allow the triggering of any other action maps.

    Otherwise I'll have to send all of my action maps through some kind of weird filter and using the Input System will become a big pain.
     
    Plaximo likes this.
  2. btschumy

    btschumy

    Joined:
    Jul 31, 2019
    Posts:
    91
    I have this same question. When I click a button, I get the button pressed event, but the code that is doing hit testing in the scene is also responding in Update().
     
  3. btschumy

    btschumy

    Joined:
    Jul 31, 2019
    Posts:
    91
    I don't know if this is the correct solution or not, but it does work. All my buttons are in a toolbar. I get the RectTransform.rect of the toolbar and exclude hit testing if the mouse position is in the rect.
     
  4. dylannorth

    dylannorth

    Joined:
    Dec 26, 2015
    Posts:
    8
    I use

    Code (CSharp):
    1. EventSystem.current.IsPointerOverGameObject()
    to check against any player raycasts that may intercept with UI objects, the GameObject in "IsPointerOverGameObject" refers to a canvas gameobject, not a world gameobject
     
    peter_the_coder likes this.
  5. btschumy

    btschumy

    Joined:
    Jul 31, 2019
    Posts:
    91
    Ah, that is much cleaner. Thanks.
     
  6. johaga

    johaga

    Joined:
    Oct 10, 2019
    Posts:
    9
    "EventSystem.current.IsPointerOverGameObject()" doesn't work with the UI Toolkit. EventSystem.current is returns null. EventSystem.current is the Unity.UI.EventSystem. The UI Toolkit though uses UnityEngine.UIElements.EventSystem which is a completely different Event System.

    And without that, I'm unsure how to raycast to detect if the mouse is over the Toolkit. I think the "Graphic Raycaster" is key here, but I dunno quite how yet.
     
  7. johaga

    johaga

    Joined:
    Oct 10, 2019
    Posts:
    9
    Oh, "Graphic Raycaster" is also incompatible with the UI Toolkit.

    Turns out a workaround is to create a script that has this in the Awake/Start somewhere:
    Code (CSharp):
    1.  
    2. var visualElement = rootDocument.rootVisualElement.Q<VisualElement>("rootVisual");
    3. visualElement.RegisterCallback<MouseOverEvent>((evt) =>
    4.         {
    5.             OverUI = evt.target != visualElement;
    6.         });
    7.  
    Where "rootVisual" is a root element in your main UI that contains the controls. Then I have "OverUI" set as a static variable.

    Ugly as sin, but it'll get the job done until the UI team + Input team can talk to each other about how to prevent Input from flowing past the UI.
     
    wolf123450 likes this.
  8. rango302

    rango302

    Joined:
    Feb 1, 2018
    Posts:
    18
    You saved my day, I search everywhere and here is a simple solution
    How did I never think about this?
     
  9. SlowpokeFarm

    SlowpokeFarm

    Joined:
    May 6, 2018
    Posts:
    24
    Too bad it doesn't work with touch input. Tried to change it to Pointer events but no luck either
     
  10. konsti1994

    konsti1994

    Joined:
    Nov 29, 2020
    Posts:
    3
    Has anyone found a solution for touch input?
     
  11. Steffen-ttc

    Steffen-ttc

    Joined:
    May 27, 2017
    Posts:
    11
    Any news regarding a touch solution?

    Edit:
    It should work with IsPointerOverGameObject(). Make sure your root visual element has its picking-mode set to ignore, else it always returns true.
     
    Last edited: Apr 23, 2022
unityunity