如何检测鼠标左键单击,而不是在UI按钮组件上单击时检测 [英] How to detect left mouse click but not when the click occur on a UI Button component

查看:187
本文介绍了如何检测鼠标左键单击,而不是在UI按钮组件上单击时检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本使GameObject在Input.GetMouseButtonUp(0)上移动.不幸的是,当我单击UI Button时,移动功能被触发,导致GameObject移动.

My script makes a GameObject move on Input.GetMouseButtonUp(0). Unfortunately, when I click on a UI Button, the move function is triggered causing the GameObject to move.

当我按下屏幕上的按钮(UI元素)时,我不希望我的GameObject移动.我想防止在单击Button之类的UI组件时移动GameObject吗?我该如何补救?另外,我想检查鼠标是否在 特定 UI元素(2个或3个按钮)上单击了

I do not want my GameObject to move when I press a button on the screen (UI element). I want to prevent GameObject from moving when the click is on a UI component such as Button? How can I remedy this? Also, I'd like to check if the mouse was clicked over specific UI elements (2 or 3 buttons)

推荐答案

阅读您的评论后.您需要的是 EventSystem.current.IsPointerOverGameObject(),用于检查指针是否通过UI.指针位于UI上时为true,否则为false.您可以将其与'!'一起使用,并且仅在鼠标不在UI上方时运行旋转代码.

After reading your comment. What you need is EventSystem.current.IsPointerOverGameObject() which checks if pointer is over UI. true when pointer is over UI, otherwise false. You can use it with '!' and run your rotation code only if the mouse is not over the UI.

对于台式机

if(Input.GetMouseButtonUp(0) && !EventSystem.current.IsPointerOverGameObject())
{
    //Your code here
}

//对于手机

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended && !EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
    //Your code here
}   

上面的解决方案应该可以,但是有一个错误.当在UI Button上按下鼠标按钮,然后将其释放到UI Button之外时,它将注册为单击.因此,基本上,该解决方案仅在单击UI Button并在UI Button上释放时才有效.如果在U Button之外发布,则原来的问题将再次出现.

The solution above should work but there is a bug. When the mouse button is pressed over the UI Button then released outside of the UI Button, it will register as a click. So, basically, the solution works only when you click on UI Button and release on UI Button. If you release outside the U Button, the original problem will be back again.

最好的解决方案是使用一个临时的boolean值,并检查最初在具有Input.GetMouseButtonDown的UI上是否按下了按钮.保存的boolean,然后可以用Input.GetMouseButtonUp(0)释放Button时使用.提供以下解决方案以与Mobile和Desktop一起使用.经过移动测试,可以正常工作.

The best solution is to use a temporary boolean value and check if button was originally pressed on a UI with Input.GetMouseButtonDown. That saved boolean, you can then use when the Button is released with Input.GetMouseButtonUp(0). The solution below is provided to work with both Mobile and Desktop. Mobile tested and it works.

bool validInput = true;

private void Update()
{
    validateInput();

    #if UNITY_STANDALONE || UNITY_EDITOR

    //DESKTOP COMPUTERS
    if (Input.GetMouseButtonUp(0) && validInput)
    {
        //Put your code here
        Debug.Log("Valid Input");
    }
    #else

    //MOBILE DEVICES
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended && validInput)
    {
        //Put your code here
        Debug.Log("Valid Input");
    }
    #endif
}

void validateInput()
{
    #if UNITY_STANDALONE || UNITY_EDITOR
    //DESKTOP COMPUTERS
    if (Input.GetMouseButtonDown(0))
    {
        if (EventSystem.current.IsPointerOverGameObject())
            validInput = false;
        else
            validInput = true;
    }
    #else
    //MOBILE DEVICES
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    {
        if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            validInput = false;
        else
            validInput = true;
    }
    #endif
}

这篇关于如何检测鼠标左键单击,而不是在UI按钮组件上单击时检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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