unity ui block physics raycast code example

Example: unity ui block physics raycast

private bool IsOverUI(TouchState touch)
        {
            PointerEventData pointerData = new PointerEventData(EventSystem.current);
            pointerData.position = touch.position;

            List<RaycastResult> results = new List<RaycastResult>();
            EventSystem.current.RaycastAll(pointerData, results);

            if (results.Count > 0)
            {
                for (int i = 0; i < results.Count; i++)
                {
                    if (results[i].gameObject.GetComponent<CanvasRenderer>())
                    {
                        return true;
                    }
                }
            }

            return false;
        }