Unity UI穿透

2020-03-05  本文已影响0人  86a262e62b0b

文档: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");
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读