检查鼠标是否点击了具有特定名称或标签的 UI 元素? [英] Check if mouse clicked on a UI element with a specific name or tag?
问题描述
EventSystem.current.IsPointerOverGameObject()
检查鼠标是否在任何 UI 元素上单击.如何检查鼠标是否点击了具有特定名称或标签的 UI 元素?
EventSystem.current.IsPointerOverGameObject()
checks if the mouse was clicked over any UI element. How can I check if the mouse clicked on a UI element with a specific name or tag?
下面的脚本使游戏对象在 Input.GetMouseButtonUp(0)
上移动.当我单击文本"等 UI 元素时,不会触发移动功能,导致游戏对象不移动.那挺好的.这里的问题是我不能考虑哪些 UI 元素.例如,我想要的是:如果用户单击带有不阻止移动脚本"标签的 UI 元素,则移动开始.但是,如果用户点击带有阻止移动脚本"标签的 UI 元素,则不会调用移动.
The script below makes a GameObject move on Input.GetMouseButtonUp(0)
. When I click on a UI element such as "Text", the move function is not triggered causing the GameObject not to move. That's good. The problem here is I cannot which UI elements are taken into consideration. For example, what I want is: if the user clicks on a UI element with tag "don't block movement script", the movement start. But, if the user clicks on a UI element with tag "block movement script", the movement won't be called.
看,我的屏幕充满了不可见"的 UI 元素(颜色 A = 0).它们是隐形的,因此我可以在需要时激活和停用它们.问题:当我触摸或点击屏幕时,我的移动代码没有被调用,因为我点击了一个不可见的 UI 元素.这就是为什么我想检查鼠标是否在特定 UI 元素(可见元素)上单击.此处,可见的 UI 元素称为 button1
和 button2
.
Look, my screen is full of "invisible" UI elements (color A = 0). They're invisible so I can activate and deactivate them when I want to. The problem: when I touch or click the screen, my moving code is not called because I've clicked an invisible UI element.
That's why I want to check if the mouse was clicked over specific UI elements (the visible ones). Here, the visible UI elements are called button1
and button2
.
点击按钮时的作用:
using UnityEngine;
using UnityEngine.UI;
public class ButtonChecker: MonoBehaviour
{
public Button button1;
public Button button2;
void OnEnable()
{
//Register Button Events
button1.onClick.AddListener(() => buttonCallBack(button1));
button2.onClick.AddListener(() => buttonCallBack(button2));
}
private void buttonCallBack(Button buttonPressed)
{
if (buttonPressed == button1)
{
Application.LoadLevel ("Example Scene 1");w
}
if (buttonPressed == button2)
{
Application.LoadLevel ("Example Scene 2");
}
}
void OnDisable()
{
//Un-Register Button Events
button1.onClick.RemoveAllListeners();
button2.onClick.RemoveAllListeners();
}
}
移动代码(同时检查是否点击 UI 元素):
Movement code (while checking if click on UI element):
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class MovementCode : MonoBehaviour {
bool validInput = true;
void Update ()
{
validateInput();
if (Input.GetMouseButtonUp (0) && validInput)
{
transform.position += new Vector3 (0.08f, 0, 0);
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended && validInput)
{
transform.position += new Vector3 (0.08f, 0, 0);
}
}
void validateInput()
{
if (Input.GetMouseButtonDown(0))
{
if (EventSystem.current.IsPointerOverGameObject())
validInput = false;
else
validInput = true;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
validInput = false;
else
validInput = true;
}
}
}
推荐答案
您的问题有两种解决方案.
You have two solutions to your problem.
1.不要通过使 alpha 0
隐藏 UI
元素,而是使用 gameObject.SetActive(false);
在按钮游戏对象上.这应该可以解决您的问题,因为事件对禁用的游戏对象不起作用.
1.Instead of hiding the UI
element by making the alpha 0
, use gameObject.SetActive(false);
on the Button GameObject. This should solve your problem because event will not work on a disabled GameObject.
2.另一种解决方案是使用EventSystem.current.currentSelectedGameObject.CompareTag
来检查UI
属于哪个标签,然后根据在那.将它添加到您的 validateInput()
函数中.
2.Another solution is to use EventSystem.current.currentSelectedGameObject.CompareTag
to check which tag the UI
belongs to then do something based on that. Add it to your validateInput()
function.
创建可见和不可见的标签
.更改您的 Buttons
以拥有这些 tags
.然后将 MovementCode
脚本中的 validateInput()
函数替换为以下代码:
Create a visible and invisible tags
. Change your Buttons
to have these tags
. Then replace the validateInput()
function from the MovementCode
script with the code below:
void validateInput()
{
if (Input.GetMouseButtonDown(0))
{
if (EventSystem.current.IsPointerOverGameObject() && EventSystem.current.currentSelectedGameObject.CompareTag("invisible"))
validInput = true;
else if (EventSystem.current.IsPointerOverGameObject())
validInput = false;
else
validInput = true;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId) && EventSystem.current.currentSelectedGameObject.CompareTag("invisible"))
validInput = true;
else if (EventSystem.current.IsPointerOverGameObject())
validInput = false;
else
validInput = true;
}
}
上面的代码将使不可见的Buttons
不会阻止移动.
The code above will make the invisible Buttons
not block the movement.
这篇关于检查鼠标是否点击了具有特定名称或标签的 UI 元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!