我正在尝试阻止射线广播通过我的按钮2d UI。当前,当按钮后面没有游戏对象时,按钮可以正常工作,但是当按钮后面有游戏对象时,它将触发对象而不是我的按钮。我尝试使用以下代码尝试检测UI,但它似乎无法正常工作,并不断返回True:
EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId));
任何帮助将不胜感激。
编辑:
我当前的代码:
void Update () {
if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)){
RegisterModelTouch();
}
}
public void RegisterModelTouch() {
// We assume that there was only one touch and take the first
// element in the array.
try {
Touch touch = Input.touches[0];
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(touch.position);
if (Physics.Raycast(ray, out hit)) {
if (hit.collider.CompareTag("Mytag")) {
//Do model stuff here
}
}
}
catch (Exception e) {
return;
}
}
答案 0 :(得分: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 bool IsPointerOverUIObject() {
// 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;
}