如何检测鼠标左键单击,但在 UI 按钮组件上发生单击时不检测 [英] How to detect left mouse click but not when the click occur on a UI Button component
问题描述
我的脚本使游戏对象在 Input.GetMouseButtonUp(0)
上移动.不幸的是,当我点击 UI Button
时,移动功能被触发导致游戏对象移动.
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 元素)时,我不希望我的游戏对象移动.当单击位于 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.true
当指针在 UI 上时,否则 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
,然后您可以在 Button
使用 Input.GetMouseButtonUp(0)
释放时使用.下面的解决方案适用于移动设备和桌面设备.移动测试,它的工作原理.
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屋!