12

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

然而,点击Button似乎是点击进入场景(在我的情况下点击导航网格)。

如何解决这个问题呢?

我一直在使用典型的 Unity3D 代码让用户参与游戏,例如

if (Input.GetMouseButtonDown(0))
  {

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

if( Input.touches.Length > 0 )
        {

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

在 iOS、Android 和桌面上似乎也是如此。

点击 UI(UnityEngine.UI.Button等)似乎是游戏的基本问题。

4

3 回答 3

28

以下是您今天在 Unity 中的操作方式:

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

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

  3. 做这个:

.

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

基本上基本上,这就是它的全部内容。

很简单:这就是您在 Unity 中处理触摸的方式。这里的所有都是它的。

添加一个光线投射器,并拥有该代码。

它看起来很容易,也很容易。但是,要做好可能会很复杂。


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


Unity 的触控技术之旅令人着迷:

  1. “早期统一”……非常容易。完全没用。根本没有工作。

  2. “当前的‘新’Unity”……效果很好。非常容易,但很难以专业的方式使用。

  3. “即将到来的未来 Unity”......在 2025 年左右,他们将使它既能实际工作又易于使用。不要屏住呼吸。

(情况和 Unity 的UI系统没什么不同。起初 UI 系统很可笑。现在,它很棒,但以专家的方式使用有点复杂。截至 2019 年,他们将再次彻底改变它。)

(网络是一样的。起初它完全是垃圾。“新”网络非常好,但有一些非常糟糕的选择。就在最近 2019 年,他们再次改变了网络。)


方便的相关提示!

记住!当您有一个包含一些按钮的全屏隐形面板时。在全屏隐形面板本身上,您必须关闭光线投射!很容易忘记:

在此处输入图像描述


作为一个历史问题:这里是“忽略 UI”的粗略快速修复,您多年前可以在 Unity中使用它......

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

几年来,你不能再这样做了。

于 2016-02-20T23:20:08.903 回答
1

我也有这个问题,我找不到非常有用的信息,这对我有用:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class SomeClickableObject : MonoBehaviour
{
    // keep reference to UI to detect for
    // for me this was a panel with some buttons
    public GameObject ui;

    void OnMouseDown()
    {
        if (!this.IsPointerOverUIObject())
        {
            // do normal OnMouseDown stuff
        }
    }

    private bool IsPointerOverUIObject()
    {
        // get current pointer position and raycast it
        PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
        eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        List<RaycastResult> results = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventDataCurrentPosition, results);

        // check if the target is in the UI
        foreach (RaycastResult r in results) {
            bool isUIClick = r.gameObject.transform.IsChildOf(this.ui.transform); 
            if (isUIClick) {
                return true;
            }
        }
        return false;
    }
}

本质上,每次点击都会检查点击是否发生在 UI 目标上。

于 2020-12-03T13:54:04.160 回答
0

不幸的是,第一个建议对我没有帮助,所以我只使用了五个所有面板标签“UIPanel”,当鼠标单击时检查当前是否有任何面板处于活动状态

    void Update()
    { if (Input.GetMouseButtonDown(0))
        {
            if (isPanelsOn()) 
            {}//doing nothing because panels is on
            else 
            {} //doing what we need to do
         }
}
public static bool isPanelsOn()
    {
        GameObject[] panels = GameObject.FindGameObjectsWithTag("UIPanel");
        foreach (var panel in panels)
        {
           if (panel.activeSelf)
            {
                return true; 
            }
        }
        return false;
    }
于 2021-03-28T19:28:03.770 回答