Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Apr 17, 2016 at 09:27 AM by Le-Pampelmuse for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Amazmohammed1 · Apr 13, 2016 at 10:14 AM · androidbuttonmobile

How Do I Add An Audio File To A Button

I Want It So That When I Tap On The Button On An Android Phone Or Tablet It Plays An Audio File

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 Le-Pampelmuse · Apr 14, 2016 at 10:07 AM 2
Share

Hi, please use normal text formatting. Starting every word with a capital letter is very annyoing to read and confusing when used in long sentences. ;)

You can use the event system of the UI. There is no need for a script.

Check Ali's anwer.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Ali-hatem · Apr 13, 2016 at 11:07 AM

you don't need any code for that if you don't have to change clip @ runtime but you have to know how to use On Click Event if not see this Make buttons do something

then attach an AudioSource to the button & add audio clip the AudioSource then create On Click event then drag & drop the button to the On Click empty field then open the drop down menu next to it highlight the AudioSource then other panel will open select Play();

Comment
Add comment · 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
0

Answer by KaushikRahul · Apr 13, 2016 at 10:21 AM

  1. You can add an AudioSource and attach it to any GameObject.

  2. Uncheck PlayOnAwake.

  3. Uncheck Loop as well if you don't want it to play in a loop.

  4. Get reference of that GameObject in your script.

  5. Play it when needed.

Script:

     using UnityEngine;
     using System.Collections;
     
     public class SoundManager : MonoBehaviour {
     
         public AudioSource sound;
        
         public AudioClip clipSound;
     
         public bool playSound;
     
         void Start()
         {
     
             sound.clip = clipSound;
     
             playSound= false;
     
         }
         
         void Update () 
        {
         
             if (playSound)
             {
     
                 sound.Play();
                 playSound= false;
     
             }
         }
     }


You can update the value of the bool whenever you want to play sound, i.e. on your click on button.

:) :)

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 Amazmohammed1 · Apr 13, 2016 at 10:51 AM 0
Share

Thanks alot

avatar image Ali-hatem Amazmohammed1 · Apr 13, 2016 at 11:11 AM 0
Share

so how the above code worked with you

avatar image KaushikRahul Ali-hatem · Apr 14, 2016 at 05:59 AM 0
Share

Its the most basic version, which a first timer can use to get around with.

There are multiple ways to achieve the same.

I do this in small projects where I don't have to handle n number of sounds but 4-5.

:) :)

Show more comments
avatar image Le-Pampelmuse · Apr 17, 2016 at 09:23 AM 2
Share

(Sorry for the previous comment, it was bugged)

Both of you are right, Ali is just giving tips for optimizing.

  • You don't need to make the AudioSource public, since no other script needs to access it. [Serializable] AudioSource sound;

  • You could use a public function and hook it up to the button:
    public void PlayButtonSound() { sound.Play(); }
    But that is just a step of optimization. Your code wouldn't check something on every Update tick that way. Using bools for if checks and switching operations is not "wrong".

There are many ways to answer this question and it is up to the OP to choose and accept the answer that suits him best. That doesn't mean it is the best, though ;)

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Unity UI - Button Slow On Some Devices? 0 Answers

how to make unlock/lock sytem for player selection? 0 Answers

Oculus Go storage file path 3 Answers

[Unity5 3D]85 SetPass Calls still running below 15FPS on Galaxy Samsung S3 0 Answers

Unable to build Android game, build error 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