How to know what UI element is being touched?
Asked Answered
J

2

0

i just started to get into mobile inputs.

i’ve learned to check if I’m touching the UI or not but I can’t find how could I get it so I know what UI element I actually are touching (ex. image, text…).

Jaques answered 2/4 at 21:7 Comment(0)
K
0

You can get EventSystem.current and change the position to touch position. Then all the raycasts you get with the RaycastAll function will give you the gameObjects that you are currently touching.

public bool TouchOnTrashButton ()//if touch phase ends on this button 
{
    //than the function will return true
    if (Input.GetTouch(0).phase == TouchPhase.Ended)
    {
        PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
        eventDataCurrentPosition.position = new Vector2(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y);

        List<RaycastResult> results = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
        if (results.Count > 0)
        {
            if (results[0].gameObject.name.Equals("TrashCanGFX"))
            {
                return true;
            }
        }
    }
    return false;
}
Keary answered 2/4 at 21:6 Comment(0)
O
0

Thank you @Keary worked perfectly.

Ohg answered 2/4 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.