- Home /
Android Button : Select (highlight) by touch, before clicking.
I have a multiplatform game with dozens of buttons in many menus.
I used the animated buttons setup to have a normal state and highlight state. Highlight will most of the time resize the button with a nice anim AS WELL as make a dedicated text box appear with extra information. So far so good. Everything works perfectly on PC.
However, as I am testing my build on android, I cannot find how to access the "highlight" state since touching the screen = click and it execute the on click () associated.
What I need is a way to select a button when it is first touched, since selected = highlight (somewhat) then if it is touched (clicked) a second time while selected then to do the on click ( ) function.
I have a few theory on how to do it, but it would require recreating every of my 80 or so buttons as well as changing everything from using the Unity GUI system to make my own C# version which would take weeks.
I am also trying a few other way to achieve the same thing. One of which is using the "button.select()" to select the button on its first click. That works. problem is I cannot seem to write a way to do: "if (thisbutton.IsSelected == true)" that just refuses to work.
I suggest to use Unity UI. Easy to implement and have various events like OnClicked, OnRelease etc. Just edit its properties to your liking.
Thanks for the answer! :) However I am already using unity UI! the problem is that i cannot get the highlight state to display when using Android since there is no "mouse over" state when using a touch device!
Answer by FireHawkX · Nov 01, 2016 at 03:48 PM
Should anyone every come here looking for an answer to something similar, here is what i eventually managed to do :
I created a special button script that could either call the function directly if running on PC, or, if on a touchscreen it would select the button first based on its name, and if it was already selected, it would call the function.
using UnityEngine.UI;
(...)
public void CallButton (string OriginalName)
//the function called when the button is clicked, the string is the name of the button in Unity
{
#if UNITY_STANDALONE || UNITY_WEBPLAYER || UNITY_WEBGL
ButtonFunction(); //IF on pc, call the function directly
#elif UNITY_IOS || UNITY_ANDROID || UNITY_WP8 || UNITY_IPHONE
if (Static.ButtonSelectedName == OriginalName)
{
ButtonFunction(); //if on touch, and button is already selected, call function.
}
else
{
Static.ButtonSelectedName = OriginalName;
//update static STRING with the name of this button
Button thisButton = GameObject.Find(OriginalName).GetComponent<Button>();
thisButton.Select (); //Select (highlight) the button.
}
#endif
}
void ButtonFunction ()
{
//anything that your button actually needs to be doing
}
This works on every single button in my project and enabled them to get selected with the first touch, then executed on the 2nd if its already selected. Hope it can help someone one day! :)
Answer by hoogemast · Oct 06, 2016 at 11:29 AM
You could create a specialized onclick method for the button that sets the ui element on hover first and then when the onclick method is called again the function is called.
so something like this.
onClickCall
{
if (firstclick)
{
firstclick = false;
//reset all other buttons set this button on hover
//could be managed with a button class variable (static) that stores all buttons
}
else
{
//call the function here
firstclick = true; //reset the firstclick of this button again after function is called
}
}
when you click an object however you should reset all other buttons somehow (maybe an array or something that is looped through?)
This is pretty much the way i was thinking of doing it... However, that means i must stop using the unityUI button on click () that i have... since all of my 80 something buttons have different things setup as their on click ()... and there are also many sliders and checkbox that uses the exact same animation mechanics.... many of which toggle the "set active" property of various gameobjects... that would mean having a seperate onclickcall.cs for every single buttons since each have a different function that is called... and that was exactly what i was trying to avoid... i already have over 90 .cs files in my game and didnt want to double that amount simply to fix the buttons...
Your answer
