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物体的名称