I want to press a Button and play a sound?
Hi, I have a created a Button and I want to make that when I click on it the button let an audio play.
Thanks
Answer by Jawchewa · May 20, 2017 at 10:37 PM
Normally, I would actually incorporate this into a script, but this is actually pretty simple, and can be achieved entirely through the the editor. What you can do is attach an AudioSource component to your button, and add the Audio Source's Play function to the button's On Click event. To do this, click the plus button on the button component's On Click event, and drag the audio source into it. Then, click the part that says "No Function", Click on Audio Source, and find the Play method. You will also need to drag your sound file from the assets folder into the AudioSource's AudioClip field. And, you will probably want to set PlayOnAwake to false. In the end, it should look something like this:
But if you really wanted to do it from script, you can attach an audio source in the same way, and do something like this:
public void Start()
{
Button b = GetComponent<Button>();
AudioSource audio = GetComponent<AudioSource>();
b.onClick.AddListener(delegate() { audio.Play(); });
}
Answer by nubick · Sep 17, 2017 at 09:05 AM
You can use following Unity editor extension for buttons click sound management: https://github.com/nubick/unity-button-sounds-editor
when ever press my rate button and play store open and when close rate button then audio play?????