美文网首页
Unity UI穿透

Unity UI穿透

作者: 86a262e62b0b | 来源:发表于2020-03-05 11:39 被阅读0次

    文档:https://docs.unity3d.com/2018.4/Documentation/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html

    PC下:

    private void Update()
    {
        //如果不带参数,则它指向鼠标左键(pointerId = -1)
        if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    
            RaycastHit raycastHit;
            if (Physics.Raycast(ray, out raycastHit))
            {
                Debug.Log(raycastHit.collider.name);
            }
        }
    }
    

    移动端下:

    void Update()
    {
        // Check if there is a touch
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
            // Check if finger is over a UI element
            if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            {
                Debug.Log("Touched the UI");
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Unity UI穿透

          本文链接:https://www.haomeiwen.com/subject/lowtrhtx.html