Detecting mouse over UI element with preview package UIElement runtime?

Does anyone know if there is a similar way to know if the mouse is over a UI element with the new UIElement runtime preview package as you can do with:
EventSystem.current.IsPointerOverGameObject()with the current UI system (so that if I click a UI element, I don’t trigger any game logic that might be tied to the same mouse button)?

3 Likes

You can register for MouseEnterEvent and MouseLeaveEvent. Search for these in:

for example use.

1 Like
#region Events
        //
        //
        //

        inlineVE.Q<TextField>().RegisterCallback<MouseEnterEvent>(
            e => (e.target as VisualElement)
                .style.backgroundColor = Color.yellow);

        inlineVE.Q<TextField>().RegisterCallback<MouseLeaveEvent>(
            e => (e.target as VisualElement)
                .style.backgroundColor = Color.clear);

        var textFieldList = textFields.ToList();
        foreach (var field in textFieldList)
            field.RegisterCallback<ChangeEvent<string>>(
                e => m_Tank.tankName =
                    (e.target as TextField).value);

        integerFields.ForEach(field =>
            field.RegisterValueChangedCallback(
                e => m_Tank.tankSize = e.newValue));

I can’t find the solution how to detect the click event is in ui or in game object after I check the demo code.
In my game, I have a character script also listen the click event and I don’t want to it will trigger when I click the UI.
Is it any other suggest code?

2 Likes

How use it with not UI gameObject?

I am curious too about how to check this without the need to implement the event listeners in all ui elements.

I believe this is what Panel.Pick() does. I’ve not tried it, but I think it would be something like this:

IPanel panel = rootElement.panel;
Vector2 panelPosition = RuntimePanelUtils.ScreenToPanel(panel, mousePosition);

VisualElement topmost = panel.Pick(panelPosition);
Debug.Log($"The top-most element mouse is over is {ve.name}");

List<VisualElement> listForAll = new(); // should store this in a field and reuse it
panel.PickAll(panelPosition, listForAll);
foreach(VisualElement ve in listForAll)
    Debug.Log($"Mouse is over {ve.name}");
1 Like

Yes it could but it would require a lot of wrapping, especially if you have a complex UI. They said the other day they are almost ready to release a new API that should solve this. Here