如何让游戏玩法忽略 Unity3D 中 UI 按钮的点击? [英] How to make gameplay ignore clicks on UI Button in Unity3D?

查看:16
本文介绍了如何让游戏玩法忽略 Unity3D 中 UI 按钮的点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UI Button(使用 UnityEngine.UI).

然而,点击按钮似乎是点击进入场景(在我的例子中点击导航网格).

如何解决这个问题?

我一直在使用典型的 Unity3D 代码来让用户进入诸如

if (Input.GetMouseButtonDown(0)){

如果我尝试这种方法也一样

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

iOS、Android 和桌面设备似乎都是这种情况.

点击 UI(UnityEngine.UI.Button 等)似乎是一个基本问题.

解决方案

以下是您今天在 Unity 中的做法:

  1. 自然地,您将在层次结构中有一个 EventSystem - 只需检查您是否有.(例如,当您添加 Canvas 时,您会自动获得其中一个;通常,Unity 项目中的每个场景都已经有一个 EventSystem,但只需检查您是否确实有一个.)

  2. 向相机添加物理光线投射器(只需单击一下)

  3. 这样做:

.

 使用 UnityEngine.EventSystems;公共类游戏玩法:MonoBehaviour,IPointerDownHandler {public void OnPointerDown(PointerEventData eventData) {答对了();}}

基本上,再次基本上,这就是全部.

很简单:这就是您在 Unity 中处理触摸的方式.这就是全部.

添加一个光线投射器,并获得该代码.

看起来很简单,做起来也很简单.但是,要做好可能会很复杂.


(脚注:在 Unity 中进行拖动的一些恐怖:


作为一个历史性问题:这里是忽略用户界面"的粗略快速解决方案,您在多年前曾经能够在 Unity 中使用...

if (Input.GetMouseButtonDown(0)) {//真的不起作用...if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())返回;答对了();}

你不能再这样做了,几年了.

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.

解决方案

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:


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.

这篇关于如何让游戏玩法忽略 Unity3D 中 UI 按钮的点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆