Using Unity 5.0.
Working on Android.
I simply have a Canvas -> with inside a Panel -> with inside a Button.
The Button has in its 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.
But when I deploy the project on an Android (4.4.2) 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.
Seems like it is triggering a sort of "long tap" and it is ignoring my normal tap. I am missing some seetings? I have not much code at all, just the function, the rest is all done via Unity inspector.
You probably suffer from the same problem I did. Check the value for "Drag Threshold" for the "Event System (script)" which you will find in the inspector once you select the EventSystem in the hierarchy.
The default value is 5 which is tiny and anything but the briefest of touches will not register as a click but as a drag. Increase the size. I use 20 and my buttons now work as expected on Android. Which reminds me I have to check this now on iOS again as well.
Good luck
Related
I have a main menu inside my unity game and for some reason when i build the game for android to run it on my phone the buttons does not work, but it's not disabled.
My scene objects:
My event system object:
Three things I can suggest:
First, make sure that on your button object, raycast target is deselected. At times, this is destabilizing the text of the button, hence making the button itself invalid.
Second, check to see if the EventSystem is working. From what I'm seeing on your screen, you might have disabled it or accidentally deleted its script while working on the scene objects. You can re-create it if necessary through GameObject>UI>EventSystem.
If these don't work, you can try the steps shown here:
https://answers.unity.com/questions/1115464/ispointerovergameobject-not-working-with-touch-inp.html
This is directly related to button objects not working with touch input devices. Hope this helps!
When I build my game to Android, buttons stop calling the onClick() function I set in the Editor. In play mode and in simulator, all works well. The only way I can make the buttons work is to make a script which adds the onclick function to the button again on startup. Is there a way to fix this without having to write a script to add it to every single button? (Buttons in other scenes dont work either)
What is the best way to touch two buttons at the same time? I am working on an app that has buttons, like a D-pad and a jump button, to move your character around. Right now I am just using normal buttons and handling them with an OnClickListener. I am having problems when I am running and need to jump at the same time, or if I am running to the right, then want to go left without having to pick my finger up. I know this is possible because it works greats on game like Sonic CD and some others. Any help would be greatly appreciated.
OnClick fires only on release. Instead use the touch event handlers so that when they touch occurs, you get the events. However, note that not all devices have multitouch, and thus not all of them will be able to handle the double-touch case correctly. They will provide touch events, but not two of them. Also note that you may receive multiple "pointers" within a touch event, and will have to decide which is "yours" for each button if that matters.
I'm new on game dev for Android.
I have a game where I need to click on moving Buttons or TextViews(not important).
I extended FrameLayout class and added some Buttons(through addView method). Then I tried to use TranslateAnimation, but it seems it doesn't updates coordinates for click event (i.e. when I click on the moving button on new position, the event is not handling, but when I click on the origin place(where it has started moving), the event catches even if the button left this place).
Question: How to create a moveable label(or button) that handles click events? Do I need to use tricks like hit testing? Or, may be I use completely wrong approach for games(e.g. I need to draw text instead of adding the views in layout)? I will be happy if you can suggest another solution.
This is limitation of the Animation in Android. They fixed that in Android 3.0. Read here for more information http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html
An excerpt:
"Finally, the previous animations changed the visual appearance of the target objects... but they didn't actually change the objects themselves. You may have run into this problem. Let's say you want to move a Button from one side of the screen to the other. You can use a TranslateAnimation to do so, and the button will happily glide along to the other side of the screen. And when the animation is done, it will gladly snap back into its original location. So you find the setFillAfter(true) method on Animation and try it again. This time the button stays in place at the location to which it was animated. And you can verify that by clicking on it - Hey! How come the button isn't clicking? The problem is that the animation changes where the button is drawn, but not where the button physically exists within the container. If you want to click on the button, you'll have to click the location that it used to live in. Or, as a more effective solution (and one just a tad more useful to your users), you'll have to write your code to actually change the location of the button in the layout when the animation finishes."
I made one simple android application and I notice next problem:
I have several controls with attached click (or i say touch) event (buttons, imageviews). Some of them are loaded from android xml file, and some I make "in fly".
Now when I scroll screen, I must 2 time click on button to make effect (click event). It seems to me that this have some connection with losing focus.
Am I right and how to solve it? I want just one click to execute some operation.
This happen only on real device (HTC Wildfire). In emulator there is no this problem.
Thanks!
Do you, by any chance, have
android:focusableInTouchMode="true"
for the Button? Because this will cause the button to need one touch to gain focus and another one to fire the click.