Detecting which UI element is at position

Hello!

I have a scroll rect in which i programmatically instantiate a variable number of buttons. When the user scrolls through the buttons, I want to display some information related to that button in another plane (for example buttons that select a level, level name, rewards, description etc.)

So for this I need to change the information whenever one of the buttons is close to the center of the screen/ scroll rect.

I read some information about raycasting for UI, but didn’t understand much.

What would be a method to do this?

Thank you!

I used a method like this:

    /// <summary>
    /// Gets the list of items clicked.
    /// </summary>
    /// <returns>The list of items clicked.</returns>
    private List<RaycastResult> GetItemsClicked()
    {
        PointerEventData pointerData = new PointerEventData(EventSystem.current);
        pointerData.Reset();
        pointerData.position = Input.mousePosition;
        List<RaycastResult> Result = new List<RaycastResult>();
        EventSystem.current.RaycastAll(pointerData, Result);
      
        return Result;
    }

Then iterate the results to find what you want:

            List<RaycastResult> ItemsClicked = GetItemsClicked();
            foreach (RaycastResult Item in ItemsClicked)
            {
                if (Item.gameObject.name == "Generator(Clone)")
                {
                    // Do something here
                    break;
                }
            }