• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
9
Question by sas_88 · Jun 04, 2015 at 06:10 AM · androiduimobiletouch

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Andres-Fernandez · Jun 04, 2015 at 06:20 AM 1
Share

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 timing 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.

2 Replies

· Add your reply
  • Sort: 
avatar image
26
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sas_88 · Jun 04, 2015 at 09:50 AM 0
Share

Thanks man @Saad_$$anonymous$$han it saves the day...

avatar image cjsawyer · Aug 26, 2016 at 04:09 PM 2
Share

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.

avatar image _joe_ · Apr 23, 2017 at 01:44 PM 0
Share

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"

avatar image
10

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!

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BowlerBitesLane · Jun 07, 2017 at 02:57 AM 1
Share

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!

avatar image Maxim_AO · Oct 30, 2017 at 08:38 AM 1
Share

GREAT THAN$$anonymous$$S!!!!

avatar image DLively · Nov 18, 2017 at 04:59 AM 1
Share

This should be green.

avatar image jidekrm · Jan 08, 2018 at 12:10 PM 1
Share

Works for me..and easier too!

avatar image seansteezy · Jan 29, 2018 at 09:40 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Do UI Buttons work the same for Touch for an Android Game? 1 Answer

Create on Gui controls for Mobile Devices (Android) 1 Answer

Moving a GUI Image to the same position as a touch 0 Answers

How to touch, move and rotate 3D objects in XZ? 1 Answer

can't use more that two buttons at the time. How do I change that? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges