【Unity】利用EventSystem判断是否点击到UI

    //是否点击UI
    bool IsRaycastUI()
    {
        //鼠标点击事件
        if (EventSystem.current == null || !EventSystem.current.IsPointerOverGameObject())
            return false;
        else
            return true;
    }

    bool IsRaycastUI02()
    {
        if (EventSystem.current == null)
            return false;
        //鼠标点击事件
        PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
        //设置鼠标位置
        pointerEventData.position = Input.mousePosition;
        //射线检测返回结果
        List results = new List();
        //检测UI
        //graphicRaycaster.Raycast(pointerEventData, results);
        EventSystem.current.RaycastAll(pointerEventData, results);
        //打印结果
        for (int i = 0; i < results.Count; i++)
        {
            Debug.Log(results[i].ToString());
        }
        //返回值>0,即点击到UI
        return results.Count > 0;
    }

 

你可能感兴趣的:(【Unity】利用EventSystem判断是否点击到UI)