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

来源:互联网 发布:淘宝流失竞店发现 编辑:程序博客网 时间:2024/05/20 12:50

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/ead7ca1f432f3c0f181f11         // 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 replacement23     /// for IsPointerOverGameObject() which does not work on Android in 4.6.0f324     /// </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/ead7ca1f432f3c0f181f31         // 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     }


0 0
原创粉丝点击