繁体   English   中英

IsPointerOverGameObject 对于触摸总是返回 false

[英]IsPointerOverGameObject always returns false for touch

IsPointerOverGameObject 对于触摸始终返回false 我已经尝试了所有我能找到的解决方案。

它在编辑器中完美运行 - 点击被阻止通过 UI,但没有移动设备此方法总是返回 false。

这是我的代码:

private static bool IsPointerOverGameObject()
    {
        bool isPointerOverGameObject = EventSystem.current.IsPointerOverGameObject();

        for (int i = 0; i < Input.touchCount; i++)
        {
            Touch touch = Input.touches[i];
            if (touch.phase != TouchPhase.Canceled && touch.phase != TouchPhase.Ended)
            {
                if (EventSystem.current.IsPointerOverGameObject(Input.touches[i].fingerId))
                {
                    isPointerOverGameObject = true;
                    break;
                }
            }
        }

        return isPointerOverGameObject;
    }

public void OnMouseDown()
{
    if (IsPointerOverGameObject())
    {
        return;
    }

    // code
}

这个修复对我有用:

 private bool IsPointerOverUIObject() {
     PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
     eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
     List<RaycastResult> results = new List<RaycastResult>();
     EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
     return results.Count > 0;
 }

从这里得到: http://answers.unity.com/answers/1115473/view.html

根据这里的统一帮助论坛。

EventSystem.current.IsPointerOverGameObject()要求传递参数中的触摸ID。 尝试将Input.touches [i] .fingerId传递为参数,以使其在移动设备中正常工作,而对于编辑器,则需要将其保留为空。

试试这个EventSystem.current.IsPointerOverGameObject(Input.touches[i].fingerId)

编辑:我不好,我没有看到您已经在我看到第一行的代码中传递了触摸ID,并认为丢失了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM