Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to ignore a tap on a Button in Android? [SOLVED]

Discussion in 'Android' started by topherbwell, Jan 12, 2016.

  1. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    Hello everyone,
    First off let me apologize if this has been answered somewhere, but all of the answers I am finding seem to be a bit older and possibly deprecated. I'd like to find the most straight forward answer to this question.

    I am making a game in which the character jumps when the mouse button (0) is clicked or, on Android, when the screen is tapped.

    Currently I am wrapping my input code in
    Code (CSharp):
    1. if(!EventSystem.current.IsPointerOverGameObject())
    2. {}
    On mac/pc, this works as expected, if you click the screen your character jumps but if you click on a UI element the click only passes to the button and the character input is ignored. On Android, the character continues to receive input in the scene through UI elements (although the UI elements also work).

    Basically, this is causing unwanted input when pausing/unpausing the game through the UI button, as well as unwanted input when UI windows are open.

    Thanks in advance to anyone who has found an easy way to work around this!
     
  2. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    I want to be able to retain the on mouse controls for desktop, I simply want taps to do what they normally do unless the tap is on a UI element.

    Seems really simple; frustrating that I'm having such a hard time finding a clear answer on this. I'm not saying Unity doesn't have the documentation on this, I'm just saying I can't seem to find it. Anyone?
     
  3. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    Anyone? Surely this is a common thing that people are dealing with?
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    I couldn't help too much without seeing exactly how input is handled on both devices, is it the same code for desktop and mobile?

    Off the top of my head, instead of checking that event system stuff, maybe have an "invisible" button which takes up the entire screen area as the highest button in the canvas hierarchy, thus making it the furthest "back" of all the buttons, then the visible buttons below it in the hierarchy, so this way a click or tap works as expected, I think the "closer" buttons intercept the input and block taps to the large button behind them. Maybe try that out...

    Edit: oh right also, make those buttons call a method in some manager script, that way your not listening for events but just directly calling some method/function from the buttons built in uhh... stuff...

    Good luck!
     
  5. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    Thanks for the feedback, but surely this is not an optimal method.

    How can unity not have a simple and direct way for a button to not receive input as the rest of the screen does? This seems like such a basic feature that should be included (and maybe is) in Unity, but I can't for the life of me find anything useful regarding this.


    Anyone have any useful info on this? Millions of touchscreen apps in the world and this is a basic feature that I cannot seem to find a simple way of fixing in Unity...
     
  6. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    Literally the controls ate GetMouseButtonDown(0), GetMouseButton(0), and GetMouseButtonUp(0). I just need these to perform the way they are intended (which works as a tap and a tap and hold on touchscreen) UNLESS the tap or click is on a button, in which case i just need the associated button function to be called.

    Again, working on desktop. How do I do this on Touch?
     
  7. wgt_jimmy

    wgt_jimmy

    Joined:
    Dec 22, 2014
    Posts:
    39
    You might want to use a level of indirection above what you have.
    You can try using CrossPlatformInput, e.g.
     
  8. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    I guess make an if statement that checks if any button has been clicked before assuming the screen elsewhere has been clicked... again I'm not totally sure how the logic is working on your code, but the way you describe it, I'd imagine that canvas trick would work fine on mobile... can't really come up with much more useful advice, sorry.
     
    topherbwell likes this.
  9. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    Again, appreciate your input, but that seems like a duct tape method to me. I thought one of the selling points of Unity was to "code once" and deploy to all these platforms? I'm so frustrated. /opens beer.


    This was just a little bug fix on my to do list today, but instead I've spent my whole day working on this. Have tried a dozen outdated solutions that no longer work. I've found countless q/a posts and threads with people trying to fix this EXACT problem but not one simple solution that works.
     
  10. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    Dear Unity, how do I click a UI button on touchscreen without registering a GetMouseButtonDown(0)? People have been asking this question for two years. What is the answer?
     
  11. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    Well it is one of the aims of unity and it's design, but some cases require special treatment. I wish unity could just read my mind and write lean code to do exactly what I think about haha.

    Anyway give my duct tape method a shot. You'll find the UI stuff is pretty performant, and even doing strange stuff like that it works pretty well. You'll notice putting like panels or any images in front of buttons or other elements will block input to the stuff behind. This is how I figure it would solve your problem, because buttons and other ui stuff works for a mouse or touch input. Even if it doesn't work perfectly I'd say it's worth trying out before disregarding the idea :p
     
    topherbwell likes this.
  12. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    I'm sure your method would work, but adding a blank button over the whole screen is not efficient. I am looking for a real fix, not something that will just "get the job done". I am trying to learn how to do this right and want to use best practices, and this seems like something that the majority of games on mobile will need to address.

    How are studios currently handling this? I literally need something that will do exactly what if(!EventSystem.current.IsPointerOverGameObject()) does, only for touch.
     
  13. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    Ok so I found a solution that is working here. In the Heirarchy, under EventSystem, I unticked the Touch Input Module (deactivated it) and in the Standalone Input Module, I ticked the "Allow Activation On Mobile Device" checkbox.

    Being that this game does not use multitouch, that is all that was needed. Two check boxes.

    Still doesn't really answer how I'd address this if the game was using more complex multi-touch controls, but it works 100% for this application.
     
    shreyanshanchlia and MD_Reptile like this.
  14. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    Awesome, wish everything was so easy to fix!
     
  15. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    I marked this as solved, but am still looking for the best answer to this... the solution I found will work for an application that only uses taps, but if I were using multi-touch controls this would not necessarily solve the issue I am having. I still think Unity needs to revisit the UI system to make this easier for mobile.
     
  16. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    475
    it is a F***ing joke that Unity F***ed this up.

    Consider how much money e're paying them. it's laughable.

    thanks for your tip above.

    Did you in fact stumble on BoredMormon's solution -- I mean his "third" solution

    Look for his video here ...

    http://forum.unity3d.com/threads/4-...ment-has-been-clicked-on.263473/#post-1745165

    I'll be interested to hear if that works fine for you in Android - I think it will. Cheers
     
    topherbwell likes this.
  17. concubicycle

    concubicycle

    Joined:
    Jan 18, 2015
    Posts:
    4
    For anyone who stumbles upon this, the right solution is:
    Code (CSharp):
    1. EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)
     
  18. Ritvik17

    Ritvik17

    Joined:
    Feb 21, 2021
    Posts:
    1
    yes the exact same issue i have faced and no there is no proper solution one solution i figured out possibly be neglecting the process here gun rotation when the touch is in certain area something like this :-

    Code (CSharp):
    1.  
    2. mouse_pos = Input.mousePosition;
    3. if (alec.GetComponent<new_player_controller>().facing_right == true)
    4. {
    5. if (mouse_pos.x < stageDimensions.x + (stageDimensions.x) * 26 && mouse_pos.y < stageDimensions.y + 180) return;
    6.  
    7.           Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) -transform.position;
    8.                 float rotZ_ = Mathf.Atan2(-difference.y, -difference.x) * Mathf.Rad2Deg;
    9.                transform.rotation = Quaternion.Euler(0f, 0f, rotZ_ + offset);
    10. //taking rotation by touch
    11.  
    12. }
    13.  
     
    Last edited: Jul 10, 2021