转载自:https://blog.csdn.net/andyhebear/article/details/51433748
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;
}
网友解决办法:
/// <summary>
/// Cast a ray to test if Input.mousePosition is over any UI object in EventSystem.current. This is a replacement
/// for IsPointerOverGameObject() which does not work on Android in 4.6.0f3
/// </summary>
private static bool IsPointerOverUIObject()
{
if (EventSystem.current == null)
return false;
// Referencing this code for GraphicRaycaster https://gist.github.com/stramit/ead7ca1f432f3c0f181f
// the ray cast appears to require only eventData.position.
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
return results.Count > 0;
}
/// <summary>
/// Cast a ray to test if screenPosition is over any UI object in canvas. This is a replacement
/// for IsPointerOverGameObject() which does not work on Android in 4.6.0f3
/// </summary>
private bool IsPointerOverUIObject(Canvas canvas, Vector2 screenPosition)
{
if (EventSystem.current == null)
return false;
// Referencing this code for GraphicRaycaster https://gist.github.com/stramit/ead7ca1f432f3c0f181f
// the ray cast appears to require only eventData.position.
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = screenPosition;
GraphicRaycaster uiRaycaster = canvas.gameObject.GetComponent<GraphicRaycaster>();
List<RaycastResult> results = new List<RaycastResult>();
uiRaycaster.Raycast(eventDataCurrentPosition, results);
return results.Count > 0;
}