(Runtime) How to determine if my mouse is on top of my UIElements UI?

I have some super basic UI I created while playing around with the tank demo UI.
I don’t want my character to move if I’m clicking on UI elements (I’m raycasting on mouse click to find the position).

I’ve found this code online, but it’s not working. I have very little experience with EventSystem, so I’m not sure how it determines what’s the current one, and maybe that’s the issue.

if (EventSystem.current.IsPointerOverGameObject()) {
    return;
}
// do regular raycasting...

Is this code supposed to work with UIElements, or is there another way to detect the mouse being on top of it?

Thank you!

1 Like

Do I need to add OnMouseEnter/Leave callbacks on my UI panels and then toggle a boolean on some kind of singleton instance so that I can easily access it in my move script?

Any pointers whatsoever? I’m sure some of you had this need before! ty.

use this in your EditorWindow Script

public void OnEnable()
{
    VisualElement visualElement = new VisualElement();
    visualElement.style.width = new StyleLength(Length.Percent(100));
    visualElement.style.height = new StyleLength(Length.Percent(100));
    visualElement.RegisterCallback<MouseEnterEvent>(x =>
    {
        Debug.Log(string.Format("mouse entered x:{0} on x:{1} y:{2}", visualElement.name,                               x.mousePosition.x,
            x.mousePosition.y));
       
    });
    rootVisualElement.Add(visualElement);
}

that should do it

1 Like

So looks like I do have to handle it myself with events. Thanks :slight_smile: