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

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

转载请保留原文链接:http://blog.csdn.net/andyhebear/article/details/51433748

UGUI 提供了一个检测是否点击在UI上的方法
EventSystem.current.IsPointerOverGameObject();
在EventSystem的标准输入Standalone Input Model下是正常的,

但是在Touch Input Module输入模式下不正常

参考网络资料,解决办法(直接上源码):


 //UGUI 提供了一个检测是否点击在UI上的方法
    //EventSystem.current.IsPointerOverGameObject();
    //但是该方法在PC上检测正常,结果拿到Android真机测试上,永远检测不到。
    //方法一, 使用该方法的另一个重载方法,使用时给该方法传递一个整形参数
    // 该参数即使触摸手势的 id
    // int id = Input.GetTouch(0).fingerId;
    //public static bool IsPointerOverGameObject(int fingerID) {
    //    return UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(fingerID);//移动输入模式下一样不行

    //}
    public static bool IsPointerOverGameObject() {
        //if (Input.touchCount > 0) {
                        
        //    int id = Input.GetTouch(0).fingerId;
        //    return UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(id);//安卓机上不行
        //}
        //else {
            //return UnityEngine.EventSystems.EventSystem.current.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);
            //Debug.Log(list.Count);
            return list.Count > 0;
       // }
    }
    //方法二 通过UI事件发射射线
    //是 2D UI 的位置,非 3D 位置
    public static bool IsPointerOverGameObject(Vector2 screenPosition) {
        //实例化点击事件
        PointerEventData eventDataCurrentPosition = new PointerEventData(UnityEngine.EventSystems.EventSystem.current);
        //将点击位置的屏幕坐标赋值给点击事件
        eventDataCurrentPosition.position = new Vector2(screenPosition.x, screenPosition.y);

        List<RaycastResult> results = new List<RaycastResult>();
        //向点击处发射射线
        EventSystem.current.RaycastAll(eventDataCurrentPosition, results);

        return results.Count > 0;
    }
    //方法三 通过画布上的 GraphicRaycaster 组件发射射线
    public bool IsPointerOverGameObject(Canvas canvas, Vector2 screenPosition) {
        //实例化点击事件
        PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
        //将点击位置的屏幕坐标赋值给点击事件
        eventDataCurrentPosition.position = screenPosition;
        //获取画布上的 GraphicRaycaster 组件
        GraphicRaycaster uiRaycaster = canvas.gameObject.GetComponent<GraphicRaycaster>();

        List<RaycastResult> results = new List<RaycastResult>();
        // GraphicRaycaster 发射射线
        uiRaycaster.Raycast(eventDataCurrentPosition, results);

        return results.Count > 0;
    }

网友解决办法:

 1     /// <summary>
 2     /// Cast a ray to test if Input.mousePosition is over any UI object in EventSystem.current. This is a replacement
 3     /// for IsPointerOverGameObject() which does not work on Android in 4.6.0f3
 4     /// </summary>
 5     private static bool IsPointerOverUIObject()
 6     {
 7         if (EventSystem.current == null)
 8             return false;
 9 
10         // Referencing this code for GraphicRaycaster https://gist.github.com/stramit/ead7ca1f432f3c0f181f
11         // the ray cast appears to require only eventData.position.
12         PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
13         eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
14 
15         List<RaycastResult> results = new List<RaycastResult>();
16         EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
17 
18         return results.Count > 0;
19     }
20 
21     /// <summary>
22     /// Cast a ray to test if screenPosition is over any UI object in canvas. This is a replacement
23     /// for IsPointerOverGameObject() which does not work on Android in 4.6.0f3
24     /// </summary>
25     private bool IsPointerOverUIObject(Canvas canvas, Vector2 screenPosition)
26     {
27         if (EventSystem.current == null)
28             return false;
29 
30         // Referencing this code for GraphicRaycaster https://gist.github.com/stramit/ead7ca1f432f3c0f181f
31         // the ray cast appears to require only eventData.position.
32         PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
33         eventDataCurrentPosition.position = screenPosition;
34 
35         GraphicRaycaster uiRaycaster = canvas.gameObject.GetComponent<GraphicRaycaster>();
36         List<RaycastResult> results = new List<RaycastResult>();
37         uiRaycaster.Raycast(eventDataCurrentPosition, results);
38         return results.Count > 0;
39     }


  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rains卍Soft

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值