IsPointerOverEventSystemObject always returns false on mobile

Calling IsPointerOverEventSystemObject works great in unity editor and standalone builds but on mobile devices (Android to be more specific) it always returns false.
Is this a bug or am I missing something here?

IsPointerOverEventSystemObject takes an int for the touch id. Mouse is the default (-1) use touch id’s to figure out if a certain finger is over a managed object.

3 Likes

Thanks Tim!
I had no idea IsPointerOverEventSystemObject took a touch id, I’ll give it another shot.

Is there a reference as to what touch ids are possible? Like, would 0 be first touch or something?

Also, wouldn’t it be easier to just not propagate touches through stuff, or make it a toggle?

No references as of yet but -1 if mouse and then the rest are the ids of Touch.fingerId

Anyone care to post a working example?

1 Like

The most basic example would be something like this:

void Update ()
        {
                if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
                        if (!EventSystemManager.currentSystem.IsPointerOverEventSystemObject (Input.GetTouch (0).fingerId)) {
                                //Handle Touch
                        }
                }
        }
3 Likes

Since 4.6 Beta 19, IsPointerOverEventSystemObject does not work anymore. Is this by design or just a bug?

This is what we were doing before to detect button touches:

// ...
EventSystem eventSystem = EventSystem.current;
return ( eventSystem.IsPointerOverEventSystemObject( fingerId )
    && eventSystem.currentSelectedObject != null );

…were fingerId is something in the range between -1 (mouse button) and Touch.fingerId.

The console says: "UnityEngine.EventSystems.EventSystem’ does not contain a definition for IsPointerOverEventSystemObject' and no extension method IsPointerOverEventSystemObject’ of type `UnityEngine.EventSystems.EventSystem’ could be found…"

The code worked fine with 4.6 Beta 17 and 18. Is there a workaround?

1 Like

I believe it’s been changed to IsPointerOverGameObject(fingerId).

1 Like

Thank you for the quick reply, adhdchris :slight_smile: I’ll try that later. Couldn’t find the information anywhere.

Have a look here…

http://unity3d.com/unity/beta/4.6/release-notes

Right… if only it was that simple. Unfortunately the release notes are far from thorough.

Very true!

That worked. Thanks again. Actually, I read the release notes several times but couldn’t find any information about this change. :eyes:

EventSystem.currentSelectedObject is also not working anymore. It’s called EventSystem.currentSelectedGameObject now.

So for the sake of completeness, the code to test for UI button touches changed to:

bool IsPointerOverGameObject( int fingerId )
{
    EventSystem eventSystem = EventSystem.current;
    return ( eventSystem.IsPointerOverGameObject( fingerId )
        && eventSystem.currentSelectedGameObject != null );
}

Hi Bivrost

I’ve been having issues with getting the UI to block raycasts from touch input (on android) and this thread looks like the best lead I have so far to solve it. Here was the thread I made:

http://forum.unity3d.com/threads/how-to-make-ui-block-raycats-mobile.271978/#post-1820891

I would really appreciate getting a bit of your expertise on the code you provided above - I’m not quite sure where to put in in my code. I think it’s the answer to my problems, I just can’t put the pieces in place!

Thanks a lot

mickyg, just combine my previous answer on getting the touch id with Bivrost’s method (use foreach if you need multitouch):

void Update ()
{
               if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
                        if (IsPointerOverGameObject (Input.GetTouch (0).fingerId)) {
                                Debug.Log("Hit UI, Ignore Touch");
                        } else {
                                Debug.Log("Handle Touch");
                        }
               }        
}

bool IsPointerOverGameObject( int fingerId )
{
    EventSystem eventSystem = EventSystem.current;
    return ( eventSystem.IsPointerOverGameObject( fingerId )
        && eventSystem.currentSelectedGameObject != null );
}
4 Likes

The fact that you can finally detect touches on a button, is, seriously, one of the biggest advances for Unity in years! At last! Thanks for the clarification here Bivrost, good one

1 Like

Thanks a bunch Chris, I had a setting ticked in the Event System (Allow Activation on Mobile Device) that was causing the code to not work. It’s solved now and working great - appreciate your quick reply!

I can’t make this solution work on 4.6.0f3 Android; was there a regression? Here’s my code:

// Ignore clicks that are over UI buttons or other visible UI elements
if (Input.touchCount > 0 && EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) {
// ignore click

but this does not work on Android because IsPointerOverGameObject() is always false. Input.touchCount and Input.GetTouch(0).fingerId all seem to be giving valid results. In fact I even tried calling IsPointerOverGameObject() for every value from -1 through 5 and the result is always false regardless of whether the click was on a UI element or not (in this case a Button).

Has anyone got this working with the new UI on Android + 4.6.0f3?

1 Like

Wouldn’t it be better if we could do these tests with a simple Vector2 screen position - rather than having to know device-specific details of the touch/click?

It looks like there’s a raycast function - but it wants a PointerEventData as input, not simple coordinates - so it’s clearly not intended for this use.

Edit: Is this function returning true if the pointer is over any canvas object now? - I seem unable to create HUD elements that are ignored by this test, settings on Canvas Groups don’t seem to change anything…

1 Like