This may look like many threads that are already out there, but keep reading and you will understand how this is different.
I have a problem where when click on a UI button, whatever 3d object is behind it gets clicked as well, and I need this not to happen, however, most people have solved this by using EventSystem.current.IsPointerOverGameObject(). The problem with that is if I use return when the mouse is over a game object, then it will also return when I click on an object only (not through UI), and this cannot be so.
I also tried using raycasting but clicking through the UI and not through the UI on the same 3D spot returns the same list of objects (it does not give me any UI objects) I’m not sure why. If anyone knows why this is happening please let me know.
Alternatively if anyone has a different solution for avoiding clicking through UI I would also like to know it.
This could use a little polish (and renaming of scripts), but something like this:
public class Test8 : EventSystem {
public bool IsPointerOverUI()
{
return IsPointerOverUI(PointerInputModule.kMouseLeftId);
}
public bool IsPointerOverUI(int pointerId)
{
if (currentInputModule == null)
return false;
return ((Test7)currentInputModule).IsPointerOverUI(pointerId);
}
}
Sorry for the bad names…
So that’s the event system
Then, the input module…
public class Test7 : StandaloneInputModule
{
public bool IsPointerOverUI(int pointerId)
{
var lastPointer = GetLastPointerEventData(pointerId);
if (lastPointer != null)
return lastPointer.pointerEnter != null && lastPointer.pointerEnter.layer == LayerMask.NameToLayer("UI");
return false;
}
}
And then usage, I think like:
if (Input.GetMouseButtonDown(0))
{
Test8 t8 = (Test8)EventSystem.current;
if (t8.IsPointerOverUI()) print("Is Over UI.");
else print("Over something else");
}
Something like that.
1 Like