10

I am making a Unity3d mobile application. And I have a problem: How to detect touch on UI, or not?

I tried this (but it doesn't work):

UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject();

and this:

private static List<RaycastResult> tempRaycastResults = new List<RaycastResult>();

public bool PointIsOverUI(float x, float y)
{
    var eventDataCurrentPosition = new PointerEventData(EventSystem.current);

    eventDataCurrentPosition.position = new Vector2(x, y);

    tempRaycastResults.Clear();

    EventSystem.current.RaycastAll(eventDataCurrentPosition, tempRaycastResults);
    
    return tempRaycastResults.Count > 0;
}
1

3 Answers 3

23

For mobile you need to pass the id of the Touch to IsPointerOverGameObject

foreach (Touch touch in Input.touches)
{
    int id = touch.fingerId;
    if (EventSystem.current.IsPointerOverGameObject(id))
    {
        // ui touched
    }
}
1
  • 1
    I ran afoul of this recently. Oddly enough, in my project, in Unity 5.3.x EventSystem.current.IsPointerOverGameObject() worked fine in iOS (and Android!), but at least in Unity 5.4.0f3, I had to add an id (in my case 0 was fine) for iOS, but not for Android.
    – livingtech
    Aug 23, 2016 at 21:43
1

Please try this :

// for Android check differently :

if(EventSystem.current.IsPointerOverGameObject(0) return false;

// for windows check as usual :

if (EventSystem.current.IsPointerOverGameObject())
    return false;
1
0

Add this to the clickable object

private MyUIHoverListener uiListener;
private Vector3 mousePos;
    
private void OnMouseDown()
{
    mousePos = Input.mousePosition;
    Debug.Log(Input.mousePosition);
}
private void OnMouseUp()
{
    //this part helps to not trigger object when dragging
    mousePos -=  Input.mousePosition;
    //Debug.Log(mousePos);
    
    if(mousePos.x < 3 && mousePos.y < 3 
        && mousePos.z < 3 && mousePos.x > -3 
        && mousePos.y > -3 && mousePos.z > -3)
    {
        //Debug.Log("NOT MOVED");
        if (!GameMan.ObjectClickBlocker)
        {
            if (uiListener.IsUIOverride)
            {
                //Debug.Log("Cancelled OnMouseDown! A UI element has override this object!");
            }
            else
            {
                // Debug.Log("Object OnMouseDown");
                StartCoroutine(LoadThisBuilding(0));
                ToggleBuildingMenu();  
            }
        }
    }
}

Add this on the object in front of the clickable object:

public class MyUIHoverListener : MonoBehaviour
{
    [SerializeField]
    public bool IsUIOverride { get; private set; }

    void Update()
    {
        // It will turn true if hovering any UI Elements
        IsUIOverride = EventSystem.current.IsPointerOverGameObject();
    }
    void OnDisable()
    {
        IsUIOverride = false;}
    }
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.