public GraphicRaycaster m_CanvasUI;
public EventSystem eventSystem;
private void Update()
{
if (Input.GetMouseButtonDown(0)) {
CheckSecondUI(Input.mousePosition);
}
}
/// <summary>
/// 检测是否有第二层UI
/// </summary>
/// <returns></returns>
public void CheckSecondUI(Vector2 pos)
{
List<GameObject> objList = new List<GameObject>();
PointerEventData eventData = new PointerEventData(eventSystem);
eventData.pressPosition = pos;
eventData.position = pos;
List<RaycastResult> list = new List<RaycastResult>();
CanvasUI.Raycast(eventData, list);
if (list.Count > 0)
{
for (int i = 0; i < list.Count; i++)
{
Debug.Log(list[i].gameObject.name);
}
}
else {
Debug.Log("没有UI");
}
}
当鼠标在屏幕上点击时,会打印出鼠标下所有的UI物体的名称
本文介绍了一种使用Unity实现的方法,该方法通过鼠标点击事件来检测屏幕上的UI元素,并打印出鼠标位置下所有UI物体的名称。具体实现包括创建公共变量以引用Canvas UI和Event System组件,以及编写自定义函数来执行UI检测。
1387

被折叠的 条评论
为什么被折叠?



