Clicking button makes Raycast go through to word space

901 Views Asked by At

I have an interactable ground like a network of hexagon tiles in my project. I am using a new UI system from Unity, UI toolkit, and clicking a UI button makes Raycast go through and interact with the tile below.

I read about some dirty solutions, but I believe there is a better solution than these: https://forum.unity.com/threads/ui-toolkit-and-raycast-block.1034938/#post-8218593

In old UI system was something like EventSystem.current.IsPointerOverGameObject

Can anyone help me?

2

There are 2 best solutions below

2
Paweł Łęgowski On

Is the clicked button working? If not you are probably missing raycast target. Make sure it is set to true on your button's image. If that's not the case you could try using:

UnityEngine.Event.current.Use();

in your buttons callback to consume the event and stop further execution.

1
User2187 On

I found the way to make EventSystem.current.IsPointerOverGameObject work.

I had a big #Screen VisualElement streching the whole screen. In the UI builder there is a property named PickingMode under the first inspector section. You just set it to ignore and it will not be detected.

If you want to check individual elements, I have no solution for that, but there is a method .Pick(Vector2) and I don't know how it works yet.

https://docs.unity3d.com/Manual/UIE-faq-event-and-input-system.html