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 list = new List();
            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 results = new List();
        //向点击处发射射线
        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();

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

        return results.Count > 0;
    }

网友解决办法:

 1     /// 
 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     /// 
 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 results = new List();
16         EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
17 
18         return results.Count > 0;
19     }
20 
21     /// 
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     /// 
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();
36         List results = new List();
37         uiRaycaster.Raycast(eventDataCurrentPosition, results);
38         return results.Count > 0;
39     }


你可能感兴趣的:(Unity3D)