Search Unity

EventTrigger PointerEnter doesn't react on mobile devices

Discussion in 'Scripting' started by TFgamesOG, May 23, 2019.

  1. TFgamesOG

    TFgamesOG

    Joined:
    Jun 2, 2017
    Posts:
    16
    Hi!

    I wanted to implement a kind of ring menu. So when the player touches and holds a finger, the buttons around the touch position appear. Now the user must drag the still pressed finger onto one of the buttons and release the touch to invoke the action behind the button.

    So far so good, it works well with EventTrigger and the
    PointerEnter
    and
    PointerExit
    events on Desktop with a mouse. But when I run it on Android, it doesn't work- the events aren't called.

    I then added a custom script, but this also has no effect:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class UIEventTrigger : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private UnityEvent eventsOnTouchEntered;
    9.  
    10.     [SerializeField]
    11.     private UnityEvent eventsOnTouchExited;
    12.  
    13.  
    14.     private bool alreadyEntered = false;
    15.  
    16.  
    17.     private void Update()
    18.     {
    19.         var eventSystem = EventSystem.current;
    20.  
    21.         if(eventSystem != null)
    22.         {
    23.             if (Input.touchCount > 0)
    24.             {
    25.                 var fingerId = Input.touches[0].fingerId;
    26.                 if (eventSystem.IsPointerOverGameObject(fingerId) && !alreadyEntered)
    27.                 {
    28.                     eventsOnTouchEntered?.Invoke();
    29.                     alreadyEntered = true;
    30.                 }
    31.                 else if(!eventSystem.IsPointerOverGameObject(fingerId) && alreadyEntered)
    32.                 {
    33.                     eventsOnTouchExited?.Invoke();
    34.                     alreadyEntered = false;
    35.                 }
    36.             }
    37.             else if(Input.touchCount == 0)
    38.             {
    39.                 if(alreadyEntered)
    40.                 {
    41.                     eventsOnTouchExited?.Invoke();
    42.                     alreadyEntered = false;
    43.                 }
    44.             }
    45.         }
    46.     }
    47. }
    48.  
    An additional problem with the custom script might be, that I check if I touch any GameObject. How can I check which object is touched?

    Thank you so much for your help!