在Unity3d中如何检测UI上的触摸?

Kna*_*ina 4 touch unity-game-engine

在make Unity3d移动应用程序中.我有一个问题:如何检测UI上的触摸?

我试试这个(但现在工作)

UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()
Run Code Online (Sandbox Code Playgroud)

还有这个

private static List<RaycastResult> tempRaycastResults = new List<RaycastResult>();

public bool PointIsOverUI(float x, float y)
{
    var eventDataCurrentPosition = new PointerEventData(EventSystem.current);

    eventDataCurrentPosition.position = new Vector2(x, y);

    tempRaycastResults.Clear();

    EventSystem.current.RaycastAll(eventDataCurrentPosition, tempRaycastResults);

    return tempRaycastResults.Count > 0;
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*nak 9

对于移动设备,您需要将Touch的ID传递给IsPointerOverGameObject

foreach (Touch touch in Input.touches)
{
    int id = touch.fingerId;
    if (EventSystem.current.IsPointerOverGameObject(id))
    {
        // ui touched
    }
 }
Run Code Online (Sandbox Code Playgroud)