使用EventSystem判断是否点击ui

#

private PointerEventData _uiPointerEventData;
private List<RaycastResult> _uiRaycastResultCache = new List<RaycastResult>();

public bool IsTouchAnyUI(Vector2 screenPos)
{
    if (null == EventSystem.current)
        return false;

    if (_uiPointerEventData == null)
        _uiPointerEventData = new PointerEventData(EventSystem.current);
    _uiPointerEventData.position = new Vector2(screenPos.x, screenPos.y);

    EventSystem.current.RaycastAll(_uiPointerEventData, _uiRaycastResultCache);

    return _uiRaycastResultCache.Count > 0;
}

public bool IsTouchUI(Vector2 screenPos, GameObject uiGo)
{
    if (null == EventSystem.current)
        return false;

    if (_uiPointerEventData == null)
        _uiPointerEventData = new PointerEventData(EventSystem.current);
    _uiPointerEventData.position = new Vector2(screenPos.x, screenPos.y);

    EventSystem.current.RaycastAll(_uiPointerEventData, _uiRaycastResultCache);

    for (int i = 0; i < _uiRaycastResultCache.Count; i++)
    {
        if (_uiRaycastResultCache[i].gameObject == uiGo)
            return true;
    }
    return false;
}

 

posted @ 2022-07-07 23:38  yanghui01  阅读(52)  评论(0编辑  收藏  举报