How to make gameplay ignore clicks on a user interface button in Unity3D?

I have a Button User Interface (using UnityEngine.UI ).

However, clicking on the Button seems to click on the scene (in my case, clicking on the navigation grid).

How to solve this problem?

I use typical Unity3D code to help a user enter a gameplay such as

 if (Input.GetMouseButtonDown(0)) { 

same if i try the approach

 if( Input.touches.Length > 0 ) { if ( Input.touches[0].phase == TouchPhase.Began ) { 

and it looks like iOS, Android and desktop.

It seems that the main problem is that the clicks in the user interface ( UnityEngine.UI.Button etc.) seem to fail in the gameplay.

+9
source share
1 answer

Here's how you do it in Unity today:

  1. Naturally, you have an EventSystem in the hierarchy (you get one of them automatically when you add a Canvas; inevitably, each scene already has one)

  2. Add a physical Raycaster to the camera (it takes one click)

  3. Do this:

,

  using UnityEngine.EventSystems; public class Gameplay:MonoBehaviour, IPointerDownHandler { public void OnPointerDown(PointerEventData eventData) { Bingo(); } } 

Basically , again, basically , that's all there is to it.

It's simple: this is how you deal with touches in Unity. That is all that needs to be done.

Add raycaster and get this code.

It looks easy and simple. However, it can be difficult to do well.


(Footnote: some drags from Unity: the horrors of OnPointerDown and OnBeginDrag in Unity3D )


Sensity's Unity journey was exciting:

  1. "Early unity" ... was extremely easy. It is completely useless. Didn't work at all.

  2. The "current" new "Unity" ... Works great. Very simple but difficult to use in an expert way.

  3. "Coming Unity" ... Around 2025, they will make BOTH actually work and be easy to use. Do not hold your breath.

(The situation is no different from the Unity UI system. At first, the UI system was ridiculous. Now it is great, but somewhat difficult to use in an expert way. Since 2019, they are going to completely change it again.)

(The network is the same. At first it was complete garbage. The β€œnew” network was / was pretty good, but it had a very poor choice. More recently, in 2019, the network changed again.)


Helpful advice!

Remember! When you have a full-screen invisible panel that contains several buttons. On the most full-screen invisible panel, you must turn off the broadcast! This is easy to forget:

enter image description here


Historical question: here is a rough and quick fix for "ignoring the user interface" that you could use in Unity many years ago ...

 if (Input.GetMouseButtonDown(0)) { // doesn't really work... if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) return; Bingo(); } 

You cannot do this anymore for several years.

+19
source

Source: https://habr.com/ru/post/1243479/


All Articles