- Home /
Touch Button android
I would like to now, if I can use the same method to create a Touch button Android using a custom GUI button,
custom GUI button:___
/ Overriding the default Control Style with one you've defined yourself / // JavaScript var customButton : GUIStyle; function OnGUI () { // Make a button. We pass in the GUIStyle defined above as the style to use GUI.Button (Rect (10,10,150,20), "I am a Custom Button", customButton); }
And this is my script:
function Update () { if(Input.touchCount >= 0) { var touch : Touch = Input.touches[0]; if(touch.phase == TouchPhase.Began) { animation["Play"].speed= 1.0; animation.Play("Play"); ResetAnimation(animation["Play"]); } } } function ResetAnimation(curAnim : AnimationState) { yield WaitForSeconds(curAnim.length); animation.Play("Idle"); }
the problem with my script is that it work with the whole screen, I need a touch button, working just when touching it with the finger on android screen cellphone, a ot the whole screen. Please help me...
I did that, put this script to an empty, does not work.
function OnGUI() { if (GUI.Button(Rect (10,10,150,20), "I am a Custom Button", customButton)) { animation["Play"].speed= 1.0; animation.Play("Play"); ResetAnimation(animation["Play"]); } } How can i fix that.?
Answer by -hiTo- · May 09, 2012 at 03:33 PM
GUI.Button works on all devices, not just PC/Mac. If I understand correctly, you want to make a button, which will trigger an animation. To do this, just create a GUI.Button, in the exact same way you explained the custom button. Your code should look something like this:
var customButton : GUIStyle;
var text : string = "";
function OnGUI()
{
GUI.Label(Rect(160,10, 200, 20), text)
if (GUI.Button(Rect (10,10,150,20), "I am a Custom Button", customButton))
{
animation["Play"].speed= 1.0;
animation.Play("Play");
ResetAnimation(animation["Play"]);
text = "Button clicked!";
}
}
function ResetAnimation(curAnim : AnimationState)
{
yield WaitForSeconds(curAnim.length);
animation.Play("Idle");
text = "Button NOT clicked...";
}
No need for an Update function at all.
I did that, put this script to an empty, does not work. can you help me....
I believe I can, but I will need more information.
What happens when you click the button? Is it clickable (does the button image change?) Do you get an error? Try making a GUI.Label that will display "clicked" when you click the button, this to ensure that the button click is being registered.
btw, if you put this piece of code into an empty script, you still need to define the variables and the ResetAnimation-function.
I will update the above script with the entire code.
i create an empty, then a java script, and put it this new code you made, the is says, error (Assets/b2.js(5,42): UCE0001: ';' expected. Insert a semicolon at the end.)
This script works as a touch fine, how can i insert the play animation script above, when touch the button play the animation?
// Draws 2 buttons, one with an image, and other with a text // And print a message when they got clicked. var btnTexture : Texture; function OnGUI() { if (!btnTexture) { Debug.LogError("Please assign a texture on the inspector"); return; } if (GUI.Button(Rect(10,10,50,50),btnTexture)) Debug.Log("Clicked the button with an image"); if (GUI.Button(Rect(10,70,50,30),"Click")) Debug.Log("Clicked the button with text"); }
Answer by Bhujanga · Oct 19, 2020 at 09:35 AM
Hi, I created several Buttons and want them to play a sound when touched on Android. No matter what I do they won't play. What can possibly be wrong? @gringofx @-hiTo-
Post a new question. This question is 8(!) years old and has nothing to do with your question.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071259im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
(Mobile) Touch anywhere on screen EXCEPT button 3 Answers
Holding GUI Button Touch to Rotate Object 0 Answers
Restart Game button for android mobile Phone help? 1 Answer
Android 3D Touch for Menu 0 Answers
Button not responsive 1 Answer