如何使游戏忽略UI按钮点击在Unity3D? [英] How to make gameplay ignore clicks on UI Button in Unity3D?

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

问题描述

我试图创建一个3D点击在Unity3D游戏中移动

I'm trying to create a 3D click to move game in Unity3D.

我添加了一个UI 按钮(使用 UnityEngine.UI ),我希望最终点击展开上运行,并且关闭。

I have added a UI Button (using UnityEngine.UI) that I would like to eventually click to toggle running on and off.

不过,点击按钮实际上似乎点击进入到现场(在我的情况 NavMeshAgent 导航),并导致我的球员移动到不同的位置。

However, clicking on the Button actually seems to be clicking through onto the scene (in my case NavMeshAgent navigation) and causes my player to move to a different location.

我如何禁用光线投射的点击UI按钮或以某种方式有UI捕捉自己的事件?

How can I disable ray casting for clicking UI buttons or in some way have the UI capture its own events?

我一直用典型的Unity3D code让用户放于游戏,如

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 )
            {

和它似乎是在iOS,Android和桌面的情况。

and it seems to be the case on iOS, Android, and desktop.

这似乎是在UI( UnityEngine.UI.Button 等)点击一个基本问题似乎落空的游戏。

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

推荐答案

旧code的粗糙和现成的解决方案是

The rough and ready solution for old code is

if (Input.GetMouseButtonDown(0))
  {
  // test carefully on all platforms
  if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
      return;
  Bingo();
  }

GOTCHYA ...

注意的是:说你的UI,你有一些典型的全屏幕空白面板,您可以用持有该只不时出现其他的事情(比如弹出窗口等)。关于全屏面板 - 你可能会意外地离开他们在

GOTCHYA...

Be careful that: say in your UI you have some typical full-screen blank panels, which you use to hold other things which only appear from time to time (eg popups, etc). Regarding the fullscreen panels - you may accidentally leave them on.

在这里输入的形象描述

撇开快捷方式,

正常的全做法,这些天是刚刚加入raycaster(点击,不可怕),并做到这一点。

The normal full approach these days is to just add a raycaster (one click, not scary), and do this

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

FULL,我这里是指,勘探需要处理倒是在Unity $ C $的C:

OnPointerDown与OnBeginDrag在Unity3D的恐怖

https://www.youtube.com/watch?v=EVZiv7DLU6E的著名的BoredMormon教程,请跳到第3部分截至今天(2月16日),没有比这更简单的解决方案。

https://www.youtube.com/watch?v=EVZiv7DLU6E the famous "BoredMormon" tutorial, skip to part 3. As of today (Feb 16) there's no easier solution than these.

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

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