Search Unity

EventSystem.current.IsPointerOverGameObject getting clicked through UI buttons in android devices

Discussion in 'UGUI & TextMesh Pro' started by tonyjoseph456, Mar 19, 2015.

  1. tonyjoseph456

    tonyjoseph456

    Joined:
    Oct 24, 2014
    Posts:
    17
    In my game, there are 2 UI Panels with 4 buttons on the screen. One panel is at the bottom, and the other panel is at the left side of the screen. The bottom panel is always visible through out the game, and the left panel pops out on clicking each of the four the UI buttons on the bottom panel. Beneath the UI there is my game scene containing the bg and other game objects. For showing the UI I have used one seperate camera and for showing the main game scene, I have used the main camera. Now coming to the problem, I need to detect the clicks on the bg of my game. So the problem is that, when I click on the UI buttons, the UI button is also getting clicked and the bg is also getting clicked through the UI button. I want only the UI button to get clicked when I click on them, and disable the click on the bg of my game.

    So for this purpose, I have used the `EventSystem.current.IsPointerOverGameObject` in my code. It is working fine on the Unity Editor, but when I tested it on Android Device, it is not giving the results, which I expected, that is bg is getting clicked through the UI button click. I tried several codes and methods which I have seen in the stackoverflow, Unity Answers and Unity Forums, but none of them are working for me in Android Devices but every thing is working fine on Unity Editor.

    My game is a 2d game and I'm using the Unity 4.6.3p1 version. I have attached a script called Tween.cs to the bg game object. Also I have attached a box collider 2D for detecting OnMouseDown. In the attached script, I have used the OnMouseDown() method. This is the code which I have used in my game.

    void OnMouseDown()
    {
    if(Input.GetMouseButtonDown(0))
    {
    foreach (Touch touch in Input.touches)
    {
    int pointerID = touch.fingerId;
    //Debug.Log ("Pointer ID "+pointerID);
    if (EventSystem.current.IsPointerOverGameObject(pointerID))
    {
    // at least on touch is over a canvas UI
    Debug.Log ("Clicked on UI");
    return;
    }
    else
    {
    Debug.Log("Clicked on BG");
    if(this.gameObject.name=="Kitchen")
    {
    Debug.Log ("Tapped on BG Kitchen");
    }
    }

    }
    }
    }

    This is one example which I got from http://gamedev.stackexchange.com/.

    Now another code:

    void OnMouseDown()
    {
    if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)
    {
    if (IsPointerOverGameObject (Input.GetTouch (0).fingerId))
    {
    Debug.Log("Hit UI button, Ignore Touch");
    }
    else
    {
    Debug.Log("Clicked on BG");
    if(this.gameObject.name=="Kitchen")
    {
    Debug.Log ("Tapped on Kitchen");
    }
    }
    }
    }
    bool IsPointerOverGameObject( int fingerId )
    {
    Debug.Log ("Finger ID"+fingerId);
    EventSystem eventSystem = EventSystem.current;
    return ( eventSystem.IsPointerOverGameObject( fingerId ) && eventSystem.currentSelectedGameObject.tag);
    }

    Can someone please help me to solve this problem. Is there some thing that I did wrong in this code. Thanks in advance.