- Home /
 
               Question by 
               socialBacons · Sep 20, 2020 at 03:59 PM · 
                androidinputtouchtouching  
              
 
              How to know what UI element is being touched?
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...).
               Comment
              
 
               
              Answer by abdullahahmetaskin · Dec 31, 2020 at 11:21 PM
You can get EventSystem.current and change the position to touch position. Then all the raycastes 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;
 }
Your answer
 
 
             Follow this Question
Related Questions
Input.GetAxis("Vertical") on touch devices. 2 Answers
Input.GetTouch(0).position.x and TouchPhase.Began 1 Answer
Google Cardboard and touch 4 Answers
TouchPhase not firing correctly - Android 3 Answers
Android Multi-Touch Issue 2 Answers
