美文网首页XUnity
Unity IsPointerOverGameObject的一个

Unity IsPointerOverGameObject的一个

作者: Wwnje | 来源:发表于2019-10-12 22:18 被阅读0次

    需求

    • 如果希望点击一个物体但是希望隔离UI,当点击到UI时不响应物体的事件,那么一般的做法都是使用Unity自带的api IsPointerOverGameObject来判断是否点击到UI,代码如下
            if (Input.GetMouseButtonDown(0))
            {
                IsOverGameObject = EventSystem.current.IsPointerOverGameObject();
                if (IsOverGameObject)
                {
                    Debug.Log("Click the UI");
                    return;
                }
    
                var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray.origin, ray.direction, out RaycastHit hit, float.MaxValue))
                {
                    if (null != hit.collider)
                    {
                        Debug.LogError(hit.collider.name);
                    }
                }
            }
    

    解析

    • 但是这个代码实际上是有问题的,比如当离开unity编辑器窗口,然后再次点击游戏内对应物体,会发现可以打印出点击物体的名字,之后再点击则是被IsPointerOverGameObject隔离了。但是我们需要的是无论什么时候都要被IsPointerOverGameObject隔离,顺着IsPointerOverGameObject查看源码,会发现如下代码
            public override bool IsPointerOverGameObject(int pointerId)
            {
                var lastPointer = GetLastPointerEventData(pointerId);
                if (lastPointer != null)
                    return lastPointer.pointerEnter != null;
                return false;
            }
    

    关键在于lastPointer.pointerEnter != null这句代码,如果在自己的UI上挂脚本并实现以下代码

        public void OnPointerEnter(PointerEventData eventData)
        {
            Debug.Log("OnPointerEnter");
        }
    

    会发现在上面有问题的代码下,从其他地方重新点击到unity游戏窗口,并点击物体,物体名字的log在OnPointerEnter log的前面,所以验证了unity内部判断是否点到ui实际上是判断是否OnPointerEnter了UI,但是遗憾的是这个顺序是不固定的。所以一旦是不固定的顺序,那么IsPointerOverGameObject在某些情况下会失灵。

    解决

    • 一个简单的解决方案是在GetMouseButtonUp时再次检测一遍,这次确保unity检测到了进入UI,代码如下
        void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                IsOverGameObject = EventSystem.current.IsPointerOverGameObject();
                if (IsOverGameObject)
                {
                    Debug.Log("Click the UI GetMouseButtonDown");
                    return;
                }
            }
    
            if (Input.GetMouseButtonUp(0))
            {
                IsOverGameObject = EventSystem.current.IsPointerOverGameObject() || IsOverGameObject;
                if (IsOverGameObject)
                {
                    Debug.Log("Click the UI GetMouseButtonUp");
                    return;
                }
    
                var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray.origin, ray.direction, out RaycastHit hit, float.MaxValue))
                {
                    if (null != hit.collider)
                    {
                        Debug.LogError(hit.collider.name);
                    }
                }
            }
        }
    

    再次运行游戏就发现第一次隔离UI是被GetMouseButtonUp捕捉到了

    相关文章

      网友评论

        本文标题:Unity IsPointerOverGameObject的一个

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