Find VisualElement at mouse position without a mouse event

Hi,
I am working a on a tooltip system and I’d like to know if there is a way to get the mouse position (and ultimately the element at that position) without having triggered a mouse event like move or click.
A particular use case that I’m trying to cover is - having an item disappear after a timeout while hovering it, should hide the tooltip as well.
My plan is to poll the the element under the cursor using schedule.Every, however I can’t find a way to detect the mouse position inside the schedule function. I tried Input.mousePosition but it returns the same position every time.
I could trigger an invalidation event when the item disappears but I don’t want to go that route because it won’t scale well.

So, is there a way?

Thanks!

At a glance I can see two relevant methods in the IPanel interface, Pick and PickAll: Unity - Scripting API: IPanel

Every visual element has a .panel property for reference.

Not sure what ‘top element at this position’ means, whether that’s the shallowest or deepest in the hierarchy, but something easily tested I suppose.

The problem is finding the position, not the element

Good point. I should read posts properly!

What position does it return? I wonder if the scheduler updates at a time before inputs have been processed, or after the current inputs have been flushed. Or can you share your code? Maybe you’re capturing something by reference in the lambda.

Have you tried the new Input System’s method of getting the current pointer position?

Maybe this is something you need to poll from a monobehaviour instead, or potentially use async/awaitables (or maybe UniTask) to continually loop around to the right point in the player loop.

It is in a lambda, and it returns an arbitrary non zero value

element.schedule.Execute(() => {
    Debug.Log(Input.mousePosition);
}).Every(200);

I have not tried the new input as I am not on Unity 6, and I don’t want the extra dependency
If the only way to do it is from a MonoBehavior, so be it, but I’d like someone from the staff to confirm so I can discontinue my search.

Correction - the code above works in Runtime but not in an Editor window

Well yes, Input.mousePosition is only intended for runtime use. For editor tools you use Event.current.mousePosition, though the current event isn’t always available and can be null under certain circumstances.

You don’t need to be using Unity 6 to use the new Input system? It’s been around for years and works fine in older versions. Even then it’s not intended to get the cursor position outside of runtime.

i am facing same issue