在Unity中注册UI上的Touch [英] Registering Touch on UI in Unity

查看:224
本文介绍了在Unity中注册UI上的Touch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我碰巧碰到了UI,则需要忽略一些功能.但是,我正在努力检测我是否真的在触摸它.

I'm needing to have some funcitonality ignored, if I have happen to touch my UI. However, I am struggling to detect if I am actually touching it or not.

我的UI使用Unity内部的本机UI.我的想法是简单地检查这些层,如果我触摸了该层上的任何内容,我将阻止任何功能的发生.

My UI makes use of the native UI inside Unity. My thought behind it was to simply check the layers and if I touched anything on that layer, I'd stop any functionality from happening.

所以我写了这个来测试它:

So I wrote this to test it:

    void Update () {
    if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) 
    {
        Ray ray = Camera.main.ScreenPointToRay( Input.GetTouch(0).position );
        RaycastHit hit;

        if ( Physics.Raycast(ray, out hit,  Mathf.Infinity, mask))
        {
            Debug.Log("hit ui");
        }
    }
}

但是,当我按下UI上的按钮(它由Canvas,Panel和一个要测试的按钮组成)时,什么也没发生.但是,如果我在场景中放置一个多维数据集并将其分配给UI层,则会显示调试日志.

However, when I press the button on my UI (it's comprised of a Canvas, Panel and a single button to test), nothing happens. However, if I place a cube in the scene and assign that to the UI layer, the debug log appears.

那是为什么?

推荐答案

我想这里的关键是:EventSystems.EventSystem.current.IsPointerOverGameObject()

无论您将鼠标悬停在UI上,它都应返回true.尝试像这样实现它:

It should return true whether you are hovering UI. Try implementing it like this:

using UnityEngine.EventSystems;
...
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) 
{
    if(EventSystems.EventSystem.current.IsPointerOverGameObject()) {
          Debug.Log("UI hit!");
    }
}

这篇关于在Unity中注册UI上的Touch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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