安卓判断是否点击在UGUI上的方法

在实际开发中,我们可能会需要区分点击的是场景中的物体还是UGUI,Unity官方提供了一个方法

EventSystem.current.IsPointerOverGameObject()

但是这个方法只能判断鼠标是否点击在了UI上,在这里提供一个在移动平台判断是否点击在UI上的方法:


bool isHitUI()
{
    PointerEventData eventData = new PointerEventData(EventSystem.current);
    List< RaycastResult > hit  = new List< RaycastResult >();
    eventData.position         = Input.mousePosition;
    eventData.pressPosition    = Input.mousePosition;
    EventSystem.current.RaycastAll(eventData, hit);

    if (hit.Count > 0)
        return true;
    else
        return false;
}

kisence

潮落江平未有风。