Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question check if the touched area on the screen is part of the UI in Enhanced Touch?

Discussion in 'Input System' started by peterbs98, Mar 17, 2024.

  1. peterbs98

    peterbs98

    Joined:
    May 24, 2020
    Posts:
    2
    Hello,

    I have this InputHandler that gets touch events from EnhancedTouch and then raises its own touch events for the rest of my game to consume.

    I want to be able to distinguish whether the touch is a UI touch or not.

    I know you can use the following with the regular input system:
    Code (CSharp):
    1. foreach (Touch touch in Input.touches)
    2. {
    3.     int id = touch.fingerId;
    4.     if (EventSystem.current.IsPointerOverGameObject(id))
    5.     {
    6.         // ui touched
    7.     }
    8. }
    But when using enhanced touch, i dont have access to the fingerId in order to pass it to IsPointerOverGameObject. How can i go about checking whether my EnhancedTouch touches are over UI elements?

    I tried to do the following:
    Code (CSharp):
    1. public bool IsClickOnUI(Vector3 location)
    2. {
    3.     var worldPoint = _camera.ScreenPointToRay(location);
    4.  
    5.     RaycastHit hit;
    6.     if (Physics.Raycast(worldPoint, out hit))
    7.     {
    8.         if (hit.transform.CompareTag("UI"))
    9.         {
    10.             return true;
    11.         }
    12.     }
    13.  
    14.     return false;
    15. }
    but it didnt work, which tells me that Physics.Raycast is completly ignoring the UI

    For reference, this is my input handler:
    Code (CSharp):
    1. using System.Runtime.CompilerServices;
    2. using System.Runtime.InteropServices.ComTypes;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.InputSystem.EnhancedTouch;
    6. using UnityEngine.InputSystem;
    7. using ETouch =  UnityEngine.InputSystem.EnhancedTouch;
    8.  
    9. public class InputHandler : MonoBehaviour
    10. {
    11.     private void OnEnable()
    12.     {
    13.         EnhancedTouchSupport.Enable();
    14.         ETouch.Touch.onFingerDown += Touch_OnFingerDown;
    15.         ETouch.Touch.onFingerMove += Touch_OnFingerMove;
    16.         ETouch.Touch.onFingerUp += Touch_OnFingerUp;
    17.     }
    18.  
    19.     private void OnDisable()
    20.     {
    21.         ETouch.Touch.onFingerDown -= Touch_OnFingerDown;
    22.         ETouch.Touch.onFingerMove -= Touch_OnFingerMove;
    23.         ETouch.Touch.onFingerUp -= Touch_OnFingerUp;
    24.         EnhancedTouchSupport.Disable();
    25.     }
    26.  
    27.     private void Touch_OnFingerDown(Finger touchedFinger)
    28.     {
    29.         if (!CameraManager.instance.IsClickOnUI(touchedFinger.screenPosition))
    30.         {
    31.             EventBus<OnFingerDownEvent>.Raise(new OnFingerDownEvent
    32.             {
    33.                 finger = touchedFinger
    34.             });
    35.         }
    36.     }
    37.  
    38.     private void Touch_OnFingerMove(Finger movedFinger)
    39.     {
    40.         if (!CameraManager.instance.IsClickOnUI(movedFinger.screenPosition))
    41.         {
    42.             EventBus<OnFingerMoveEvent>.Raise(new OnFingerMoveEvent
    43.             {
    44.                 finger = movedFinger
    45.             });
    46.         }
    47.     }
    48.  
    49.     private void Touch_OnFingerUp(Finger lostFinger)
    50.     {
    51.         if (!CameraManager.instance.IsClickOnUI(lostFinger.screenPosition))
    52.         {
    53.             EventBus<OnFingerUpEvent>.Raise(new OnFingerUpEvent
    54.             {
    55.                 finger = lostFinger
    56.             });
    57.         }
    58.     }
    59. }
     
    Last edited: Mar 17, 2024
  2. peterbs98

    peterbs98

    Joined:
    May 24, 2020
    Posts:
    2
    Hey, its me again, this will probably be a dead post, but im sure someone will come from 2-3 years in the future for this same issue, this is the only solution i found that works, basically u get the Finger.currentTouch.screenPosition, and pass it to this function:
    Code (CSharp):
    1.     public bool IsClickOnUI(Vector3 location)
    2.     {
    3.         PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
    4.         pointerEventData.position = location;
    5.  
    6.         List<RaycastResult> results = new List<RaycastResult>();
    7.         EventSystem.current.RaycastAll(pointerEventData, results);
    8.         for (int i = 0; i < results.Count; i++)
    9.         {
    10.             if (results[i].gameObject.CompareTag("UI"))
    11.             {
    12.                 return true;
    13.             }
    14.         }
    15.  
    16.         return false;
    17.     }
     
    RayPrince likes this.
  3. RayPrince

    RayPrince

    Joined:
    Jan 29, 2021
    Posts:
    2
    Thank you, this has saved hours of hacks. Took sooner that the expected 2-3 years.