- Home /
UI Buttons Working on Android.
Add Button script the OnClick() with a GameObject linked. The GameObject has a script with a function.
When I tap the button I simply trigger the function, which is working properly on Unity.
In Android device, it works only if you tap and leave the finger very fast. Otherwise if you tap and wait just a second, when you remove the finger the OnClick() is not called anymore.
That's because it uses a tap. If you keep the finger down for "just a second", it's not a tap. Take a look at TouchPhase to see the different states of a touch.
From my experience, the best way of controlling UI elements with your personal ti$$anonymous$$g is using the events of the Selectable class. You can use OnPointerDown to start a timer and OnPointerUp to see if timer has ended. That way you can control how long you can press the button to be considered a click. Although I haven't tried those with UI elements in android, so if you do try, please share the results.
Answer by Saad_Khan · Jun 04, 2015 at 08:40 AM
You should use Event Triggers if you are using the latest UI, instead of the OnClick() method
1) Add an event trigger component to the button
2) Add a new event ( on the event trigger component) from the options select "Pointerdown" or "PointerUp"(depending on whether you want to trigger it on touch or release, respectively)
3) Hook the corresponding function that you'd like to call, into this trigger
This post needs a few thousand more upvotes. The fact that buttons don't work correctly out of the box on Unity's most popular platform is a little ridiculous. This fixed the problem for me. I also found that increasing TouchInput$$anonymous$$odule's Input Actions Per second helped the problem a bit too.
Well it does work better but anybody though of the why? Is the UI Event sytem on mobile not scaling with the canvas? I feel that the slightest movement in a click is breaking the actual "click"
Answer by glitchers · Nov 25, 2016 at 11:27 AM
I found an alternate solution for this.
It's true you must tap quickly to trigger a click but I found the most common cause for a button click not firing was drag. For me the tap timer is sufficient (probably around 300ms is standard).
This problem was specifically an issue in more complex hierarchies especially scroll views. Fortunately the issue is incredibly easy to fix.
If you change the "Drag Threshold" (`pixelDragThreshold`) of your EventSystem
to a higher value it will fix a lot of the Android button issues. The default value is 5
but I use 44
based on Apple's Human Interface guidelines but whatever works for you.
Also for buttons I found it important to disable raycastTarget
on internal elements, say like any internal text or images.
I went along nearly implementing my own button solution for this but I found this forum post which shed light for me.
Hope that helps anyone else who comes to this!
As far as I can tell this is the correct solution. This seems to fix the issue application-wide by changing a single value. Thanks!
This is definitely what I've been looking for, for like 2 years... thanks @glitchers! When combined with the removing raytracing on text and interal stuff, the button will work very nicely on mobile.