Комментарии:
Script so copy and paste
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[RequireComponent(typeof(Button))]
public class ClickSound : MonoBehaviour
{
public AudioClip sound;
private Button button { get { return GetComponent<Button>(); } }
private AudioSource source { get { return GetComponent<AudioSource>(); } }
// Use this for initialization
void Start ()
{
gameObject.AddComponent<AudioSource>();
source.clip = sound;
source.playOnAwake = false;
button.onClick.AddListener(() => PlaySoud());
}
// Update is called once per frame
void PlaySoud ()
{
source.PlayOneShot (sound);
}
}
Sincs
ОтветитьThis was so easy to understand!!
ОтветитьThanks you so much you helped me a lot I am having a so much problem related to this
Ответитьty
ОтветитьThank you very much
ОтветитьThanks; very helpful!
ОтветитьHi Lena great tutorial this is still applicable today. The => is for lamda expressions. Is there another tutorial page you recommend for learning their usefulness particularly for C# / Unity development
ОтветитьThank you for this! Now I need to teach myself how to make a hover sound.
ОтветитьHi.
I have 2 buttons attached to a game object. both with different audio sources and i use On trigger Play one shot. How to prevent unity to playback both at the same time if user presses both. My goal is to have the other button wait for the first to finish the audio playback.
Thank you !!! :)
I’ve taken a picture of my steelpan (caribbean steel drum) instrument. I’d like to develop a simple app that plays samples of the instrument when notes are pressed on the smartphone touchscreen.
Any ideas much appreciated.
u the cuttest sound girl ive heard sweetheart
ОтветитьThanks a lot!
Ответитьthx a lot
ОтветитьWelp, I can only use this script for one button, because if I drag it into another one, the old one disappears..
ОтветитьHow would you do this for hover?
ОтветитьOMG! Thank you for writing the script this way. I LOVE that you are manually coding the creation of the component vs how almost all other tutorials tend to teach. Keep up the good work! Got yourself another new sub!
ОтветитьThanks soo much, very sexy voise)))
ОтветитьHello =)
Instead of onclick when using the animation transition state, like highlighted.. Is it the same procedure?.. Like play blipp sound when hovering over the button and then not hover, and then hover again and bipp sound yee ? :D
thank you :)
ОтветитьWhy do you need require component if you know for sure it's a button? Same question for the awake.
ОтветитьThank you so much!!
ОтветитьThank you so much !
ОтветитьGreat video! Quick and to the point.
ОтветитьThanks for creating this tutorial, Lena! Clear and straight to the point! I'm using onClick in the Event Trigger Component, but your argument about the re-usability of the script is a good one! Two thumbs up! :)
ОтветитьNice very useful start point for better audio controll
ОтветитьThank you very much @Lena Florian!
ОтветитьNice tutorial. nice to see you use a lambda expression which is something i need to understand more. thanks for the links too.
ОтветитьWtffff how can you write script so damn fast without making mistakes???
Ответитьthank you so much, saved me a lot of time
ОтветитьHi, I am dealing with error. I have 4 buttons but only one works . I have been trying other methods to add sound too but same, no sound was heard when clicked. Can you please help me? With the buttons, I added script for other functions as well . Thanks :)
ОтветитьMy menu disappears as soon as you click a button so Unity wasn't playing the audio. I modified the tutorial script as follows:
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Button))]
public class ClickSound : MonoBehaviour {
public AudioClip sound;
private Button button { get { return GetComponent<Button>(); } }
private static AudioSource soundPlay;
void Start ()
{
button.onClick.AddListener(() => PlaySound());
}
void PlaySound()
{
soundPlay = Instantiate(gameObject.AddComponent<AudioSource>());
soundPlay.clip = sound;
soundPlay.playOnAwake = false;
soundPlay.PlayOneShot(sound);
DestroyObject(soundPlay, 1f);
}
}
Very smart tutorial that goes off the beaten path of other tutorials showing the same thing.
Ответитьusing UnityEngine;
using System.Collections;
using UnityEngine.UI;
[RequireComponent(typeof(Button))]
public class ClikSoud : MonoBehaviour {
public AudioClip sound;
private Button button {get{return GetComponent<Button>();}}
private Audiosource source {get{return GetComponent<Audiosource>();}}
// Use this for initialization
void Start () {
gameObject.AddComponent<Audiosource>();
source.clip = sound;
source.playOnAwake = false;
button.Input.touchCount.AddListener(()=> PlaySound());
}
// Update is called once per frame
void PlaySound () {
source.PlayOneShot(sound);
}
}
thanks :}
ОтветитьThis was really great and straight to the point - thanks so much Lena! :)
ОтветитьYou could do the same with no scripting at all.
1) Add an AudioSource component to your button object.
2) Add your sound clip to the "AudioClip" field on the AudioSource component.
3) On your ButtonScript component, under OnClick(), press the + button.
4) In the left field (where it says None(object)) drag your button from the Hierachy window
5) To the right of that, click on the pop up, scroll down to AudioSource, in that sub menu, choose Play().
-done. Make sure in your audio component "play on awake" is deselected.
Just tnx. U rly save me!
ОтветитьHey! Thanks for a great and fast tutorial, short and precise.
I'm using this script to add sound to my buttons, however, the buttons that change my scenes instantly destroys the AudioSource. Is it possible to continue the sound through a change of scene within this same script?
Hey! Thanks for the Tutorial - one quick question: Why do I have to set source.clip = sound;? Later on, source.PlayOneShot(sound) is used - I don't think setting it there is really necessary, or is it?
Ответитьnice
ОтветитьExactly what I was looking for thank you lena!
ОтветитьGreat stuff! thanks
Ответитьthanks a buttload for posting this! im somewhat new and i just wanted to make a quick soundboard and i was over complicating the process and than i found your vid! subbed
ОтветитьI've a problem, I don't have the function onClick.. I dunno why, i've the same lena has, using UnityEngine.UI
button.onClick.AddListener(() => PlaySound());
onClick stays in red and says it does not contain a definition for onClick and no estension method accepting a first arguyment of type "button" could be found (are you missin a using directive or an assemly reference?)
Thank you for your tutorial, very helpful.
Ответитьmute sound tutorial please?
ОтветитьHi Lena Florian one question... regard to sound button in Android devices . How do you do dissapear an annoying latency when it pushes the button in the screen ?
ОтветитьNice, effortless video, and you get to the point right away. Well done, and thanks.
Ответить