Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
768 views
in Technique[技术] by (71.8m points)

c# - How to make gameplay ignore clicks on UI Button in Unity3D?

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

However, clicking on the Button seems to be clicking through onto the scene (in my case clicking a nav mesh).

How to solve this problem?

I've been using typical Unity3D code to get user in put in 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 seems to be the case on iOS, Android, and desktop.

It seems to be a basic problem that clicks on the UI (UnityEngine.UI.Button etc) seem to fall through to the gameplay.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's how you do it in Unity today:

  1. Naturally you'll have an EventSystem in the hierarchy - just check that you do. (You get one of those automatically when, for example, you add a Canvas; usually, every scene in an Unity project already has an EventSystem, but just check that you do have one.)

  2. Add a physics raycaster to the camera (that takes one click)

  3. Do this:

.

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

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

Quite simply: that is how you handle touch in Unity. That's all there is to it.

Add a raycaster, and have that code.

It looks easy and it is easy. However, it can be complicated to do well.


(Footnote: some horrors of doing drags in Unity: Horrors of OnPointerDown versus OnBeginDrag in Unity3D )


Unity's journey through touch technology has been fascinating:

  1. "Early Unity" ... was extremely easy. Utterly useless. Didn't work at all.

  2. "Current 'new' Unity" ... Works beautifully. Very easy, but difficult to use in an expert manner.

  3. "Coming future Unity" ... Around 2025 they will make it BOTH actually work AND be easy to use. Don't hold your breath.

(The situation is not unlike Unity's UI system. At first the UI system was laughable. Now, it is great, but somewhat complex to use in an expert manner. As of 2019, they are about to again totally change it.)

(The networking is the same. At first it was total trash. The "new" networking is/was pretty good, but has some very bad choices. Just recently 2019 they have changed the networking again.)


Handy related tip!

Remember! When you have a full-screen invisible panel which holds some buttons. On the full-screen invisible panel itself, you must turn off raycasting! It's easy to forget:

enter image description here


As a historic matter: here is the rough-and-ready quick-fix for "ignoring the UI", which you used to be able to use in Unity years ago...

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

You cannot do this any more, for some years now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...