Unity 5 UI Tutorial - Button sounds

Unity 5 UI Tutorial - Button sounds

Lena Florian

8 лет назад

87,166 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

TH GAMER
TH GAMER - 09.08.2016 10:33

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

    }

}

Ответить
ИСТОРИЯ ПОИСКА #ВМИРЕ
ИСТОРИЯ ПОИСКА #ВМИРЕ - 09.12.2020 00:57

Sincs

Ответить
N
N - 19.10.2020 05:54

This was so easy to understand!!

Ответить
GAMOGIN
GAMOGIN - 03.10.2020 19:12

Thanks you so much you helped me a lot I am having a so much problem related to this

Ответить
SLU
SLU - 04.08.2020 02:03

ty

Ответить
John Rodrigues
John Rodrigues - 10.07.2020 06:13

Thank you very much

Ответить
Albert Gregl
Albert Gregl - 27.04.2020 12:22

Thanks; very helpful!

Ответить
firepro20
firepro20 - 11.04.2020 19:42

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

Ответить
Joey McDaniel
Joey McDaniel - 11.04.2020 00:30

Thank you for this! Now I need to teach myself how to make a hover sound.

Ответить
Edgar
Edgar - 30.08.2019 00:13

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 !!! :)

Ответить
UrbanSmash
UrbanSmash - 14.08.2019 11:16

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.

Ответить
구독자 500명 되면 이 ㅈ같은 새끼 감옥 감
구독자 500명 되면 이 ㅈ같은 새끼 감옥 감 - 03.08.2019 21:26

u the cuttest sound girl ive heard sweetheart

Ответить
Federico Arrico
Federico Arrico - 17.04.2019 16:13

Thanks a lot!

Ответить
MiniClawW
MiniClawW - 30.03.2019 01:53

thx a lot

Ответить
The Yeet
The Yeet - 10.02.2019 03:47

Welp, I can only use this script for one button, because if I drag it into another one, the old one disappears..

Ответить
Roger Braun
Roger Braun - 24.10.2018 16:12

How would you do this for hover?

Ответить
IAm TheBlurr
IAm TheBlurr - 16.07.2018 22:44

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!

Ответить
Александр Колесник
Александр Колесник - 15.07.2018 17:42

Thanks soo much, very sexy voise)))

Ответить
AstralStorm Game Studios
AstralStorm Game Studios - 26.05.2018 00:21

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

Ответить
high class trash
high class trash - 18.05.2018 09:55

thank you :)

Ответить
Shay Shay
Shay Shay - 17.04.2018 21:11

Why do you need require component if you know for sure it's a button? Same question for the awake.

Ответить
박준식
박준식 - 12.04.2018 09:35

Thank you so much!!

Ответить
Kamal Ariff
Kamal Ariff - 17.03.2018 11:09

Thank you so much !

Ответить
guillermo romero
guillermo romero - 10.03.2018 13:17

Great video! Quick and to the point.

Ответить
Joe Guch
Joe Guch - 04.03.2018 00:38

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! :)

Ответить
Richard
Richard - 26.01.2018 00:57

Nice very useful start point for better audio controll

Ответить
Ruben Salas
Ruben Salas - 04.01.2018 21:53

Thank you very much @Lena Florian!

Ответить
ky killi
ky killi - 18.11.2017 19:25

Nice tutorial. nice to see you use a lambda expression which is something i need to understand more. thanks for the links too.

Ответить
Daniel_The_F1rst
Daniel_The_F1rst - 11.11.2017 18:55

Wtffff how can you write script so damn fast without making mistakes???

Ответить
Berk Can
Berk Can - 30.08.2017 21:43

thank you so much, saved me a lot of time

Ответить
Wany Shah
Wany Shah - 30.06.2017 10:08

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

Ответить
General Chad
General Chad - 24.06.2017 23:30

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

Ответить
Steve Jones
Steve Jones - 26.05.2017 17:30

Very smart tutorial that goes off the beaten path of other tutorials showing the same thing.

Ответить
RealKirill Tv
RealKirill Tv - 13.05.2017 16:19

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

Ответить
Boo Mong
Boo Mong - 03.05.2017 07:04

thanks :}

Ответить
Vicky P
Vicky P - 11.04.2017 20:55

This was really great and straight to the point - thanks so much Lena! :)

Ответить
Dana Muise
Dana Muise - 10.04.2017 02:57

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.

Ответить
Семён Баранов
Семён Баранов - 14.03.2017 19:21

Just tnx. U rly save me!

Ответить
The Wedgwood
The Wedgwood - 11.02.2017 02:17

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?

Ответить
Manuel Bergmann
Manuel Bergmann - 22.01.2017 23:47

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?

Ответить
Chucki Cky
Chucki Cky - 12.01.2017 13:02

nice

Ответить
TheH8ter
TheH8ter - 10.01.2017 18:16

Exactly what I was looking for thank you lena!

Ответить
Mrsonicthunder
Mrsonicthunder - 10.01.2017 06:25

Great stuff! thanks

Ответить
Captain Skizzy
Captain Skizzy - 08.01.2017 09:01

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

Ответить
BerserkerLP
BerserkerLP - 28.12.2016 22:00

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

Ответить
Александр Чувашов
Александр Чувашов - 28.12.2016 02:33

Thank you for your tutorial, very helpful.

Ответить
Wenny Faizza
Wenny Faizza - 25.12.2016 04:55

mute sound tutorial please?

Ответить
Omi
Omi - 30.11.2016 03:48

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 ?

Ответить
Challseus
Challseus - 28.11.2016 05:03

Nice, effortless video, and you get to the point right away. Well done, and thanks.

Ответить