- Home /
Check if UI was clicked with unity new input system 1.2.0
I've run into some problems when updating my project to Unity 2020.3.25f1. The update has made some changes to the input system which affects the way I am checking if the mouse click was performed over UI or not.
public class ClickHandler : MonoBehaviour
{
private Controls controls;
private void Awake()
{
//Get the new control system and set the controls needed for this script
controls = new Controls();
controls.Player.MouseClick.performed += _ => OnMouseClicked();
}
private void OnEnable() => controls.Enable();
private void OnDisable() => controls.Disable();
public void OnMouseClicked()
{
//ignore if UI was clicked.
if (UIManager.Instance.IsMouseOverUI())
{
return;
}}
In UIManager script:
public bool IsMouseOverUI()
{
return EventSystem.current.IsPointerOverGameObject();
}
With the new version I now recieve the following warning:
Calling IsPointerOverGameObject() from within event processing (such as from InputAction callbacks) will not work as expected; it will query UI state from the last frame
I understand that I can solve this by moving the IsPointerOverGameObject() into Unity Update() function, which guarantees the logic to run each frame. I fail to understand how to make this work with the new eventsystem, where I check inputs through events rather than an update polling-approach.
What is a good way to use the new input system and check if UI was pressed or not? Thanks in advance!
Answer by perf401 · Jan 02 at 12:04 PM
Honestly, i don't know the answer, but i would personally try to use EventSystem.current.currentSelectedGameObject != null instead, maybe, with some adjustments, it will magically work. Hope you get better answers.
The selected object and the object which mouse cursor is hovering over are completely different things if you think about it.
Answer by maross334 · Apr 16 at 03:10 AM
See if you can implement pointer events. ex:
public class Foo : MonoBehavior, IPointerEnterHandler, IPointerExitHandler, IPointerUpHandler, IPointerDownHandler
I haven't used it in the new input system, but I think it should work.
otherwise, you could implement: https://docs.unity3d.com/ScriptReference/UIElements.IPointerEvent.html
Your answer
Follow this Question
Related Questions
Drag handler is blocking image IPointerClick but not button OnClick 0 Answers
how to get two pause menus or inventory menus for two diff game pads with new input system? 0 Answers
How do I detect when UI events are triggered on *any object*? 1 Answer
How to get UI elements such as Scroll Rect to work with new input system? 0 Answers
How to get IsPointerOverGameObject(touchID) object reference 0 Answers