unity获取鼠标下有哪些UI物体
1个回答
展开全部
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");
}
}12345678910111213141516171819202122232425262728293031323334
当鼠标在屏幕上点击时,会打印出鼠标下所有的UI物体的名称
{ 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");
}
}12345678910111213141516171819202122232425262728293031323334
当鼠标在屏幕上点击时,会打印出鼠标下所有的UI物体的名称
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询