Unity之UI穿透

NGUI穿透

NGUI出现穿透的时候,通过UICamera.hoveredobject进行判断是否是在UI上即可。

问题 方法
UI穿透到了下方的非UI物体 通过EventSystem.current.IsPointerOverGameObject(),if(!EventSystem.current.IsPointerOverGameObject())进行UI逻辑
上一层UI穿透到下一层UI(常出现在显示一个面板,点击面板外出现穿透) 在整个面板下方加上一层Image的覆盖,并且接受射线检测
游戏物体穿透到了UI(UI被挡住但是却被触发) Canvas 上的 GraphicRaycaster ,通过设置Blocking Object 和 Blocking Mask ,通过这两个设置可以让点击反应只在游戏物体上触发,而不会穿透到UI上
//第一种情况
void Update () {
        //click on the 3D object
        if(Input.GetMouseButton(0))
        {
            Click();
        }
    }
    private void OnMouseDown()
    {
        Debug.Log("Capsule");
    }
    private void Click()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;
        if(Physics.Raycast(ray, out hitInfo))
        {
            //解决UI穿透到物体上
            if(!EventSystem.current.IsPointerOverGameObject())
            {
                Debug.Log("On Cube");
            }
            Debug.Log(hitInfo.collider.gameObject.name);
        }
    }

第三种情况,解决物体穿透到UI

posted @ 2024-08-14 15:58  多见多闻  阅读(5)  评论(0编辑  收藏  举报