Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by gringofx · May 09, 2012 at 02:03 PM · androidbuttontouch

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

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 gringofx · May 09, 2012 at 05:44 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 6 · 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 gringofx · May 09, 2012 at 06:10 PM 0
Share

I did that, put this script to an empty, does not work. can you help me....

avatar image gringofx · May 09, 2012 at 06:15 PM 0
Share

unknown identifier 'custombutton'

avatar image -hiTo- · May 09, 2012 at 06:17 PM 0
Share

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.

avatar image gringofx · May 09, 2012 at 07:35 PM 0
Share

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

avatar image gringofx · May 09, 2012 at 08:23 PM 0
Share

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"); }

Show more comments
avatar image
0

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-

Comment
Add comment · Show 1 · 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 -hiTo- · Oct 24, 2020 at 12:01 AM 0
Share

Post a new question. This question is 8(!) years old and has nothing to do with your question.

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.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges