Unity功能记录(十三) ------ 移动端判断是否点在UGUI组件上

一不小心踩了这个坑,因此记录一下

UGUI 提供了一个检测是否点击在UI上的方法 EventSystem.current.IsPointerOverGameObject(); 在EventSystem的标准输入Standalone Input Model下是正常的,但是在Touch Input Module输入模式下不正常(编辑器中正常,Android端不正常)

解决方案:

 public static bool IsPointerOverGameObject()
    {
        PointerEventData eventData = new PointerEventData(UnityEngine.EventSystems.EventSystem.current);
        eventData.pressPosition = Input.mousePosition;
        eventData.position = Input.mousePosition;

        List<RaycastResult> list = new List<RaycastResult>();
        UnityEngine.EventSystems.EventSystem.current.RaycastAll(eventData, list);
        return list.Count > 0;
    }

已测试有效

参考:

UGUI -(unity3d 5)判断是否点击在UI 上 Bug,IsPointerOverGameObject()在移动输入模式检测失败

猜你喜欢

转载自blog.csdn.net/dengshunhao/article/details/86563469