Uploaded August 2019 | Updated September 2026, 2 weeks ago
Consider purchasing my ULTIMATE UDEMY COURSE with great discount and get access to all of downloadable resources from all of tutorials once and forever
udemy.com/how-to-make-games-with-unity-software/?couponCode=14-99-SALE-GOOD-LUCK
I play games here - twitch.tv/zzotoff
You are very welcome :-)
In this video I will show you how to play different sounds when UI button is pressed down and released.
I start from an empty project and set it up real quick. Here I have a couple of button sprites which represent pressed and release states, button sound script and a couple of sound clips.
First I create a new UI button. Anchor it at the center of the canvas holding Alt + Shift keys. Here we go. Double click it to focus on it. I don’t need text here so I remove it. Button is blank by default so to change that I D&D one of button sprites which represents released state into source image slot here. Button is stretched. One of the ways to fix that is to check in preserve aspect option here. I make it bigger. By the way once canvas is created I like to set its scale mode to scale with screen size so UI elements will be scaled depending on screen size. I make button bigger holding shift key to scale it proportionally. Put it at the center. Ok. By default all of button states are visualized by color tint. In my case I want button states to be represented by different sprites. So I change transition option to color swap and D&D sprite which is responsible for down state into pressed sprite slot here. Very well. Sounds playback will be controlled by button sounds script. Let’s take a look at it.
Here I have a couple of audioclip variables which are button down sound and button up sound which will be played when button is pressed and released. Serialize field attribute allows me to assign these variables in inspector. Also here I have audio source variable which allows me to control an audio source component which will be attached to button to play those sounds. In start method I take a control over that audio source component. Play down sound method will be invoked when button is pressed down. When it happens button down sound is played. Play up sound method will be invoked when button is released. In this case button up sound is played. Pretty simple. D&D this script to button. Select button and lets give it final tunings. Here is button sounds script attached. D&D corresponding sound clips into their slots. In my case O goes into button down sound slot and OU goes into button up sound slot. Ok. To play sounds button needs an audio source component. So I add one to the button. Here we go. To make it possible to invoke those methods from button sounds script button needs an event trigger component which will detect when I click and release my button. Here it is. This component has to be tuned. Add new event type which will be pointer down. It triggers when button is pressed down. Hit plus sign to create a kind of response to this event. D&D button into this slot and here in function drop down select play down sound method from button sound script attached to our button. Now this method will be invoked when button is pressed down. Add new event type which is pointer up one. This event takes place when button is released. Button is already added so select play up sound from button sounds script.
If you like what I'm doing then you can support me through
patreon.com/alexanderzotov
or here
paypal.me/AZotov
Thank you)
This video is about: unity 2d tutorial, How To Play Different Sounds When You Press And Release UI Button, unity press button, unity button, unity ui button, unity press button sound, unity button sound, unity play sound when button press, unity sound, unity button sound, unity button press sound, unity button release sound, unity button tutorial, unity button animation.
Consider purchasing my ULTIMATE UDEMY COURSE with great discount and get access to all of downloadable resources from all of tutorials once and forever
udemy.com/how-to-make-games-with-unity-software/?couponCode=14-99-SALE-GOOD-LUCK
I play games here - twitch.tv/zzotoff
You are very welcome :-)
In this video I will show you how to play different sounds when UI button is pressed down and released.
I start from an empty project and set it up real quick. Here I have a couple of button sprites which represent pressed and release states, button sound script and a couple of sound clips.
First I create a new UI button. Anchor it at the center of the canvas holding Alt + Shift keys. Here we go. Double click it to focus on it. I don’t need text here so I remove it. Button is blank by default so to change that I D&D one of button sprites which represents released state into source image slot here. Button is stretched. One of the ways to fix that is to check in preserve aspect option here. I make it bigger. By the way once canvas is created I like to set its scale mode to scale with screen size so UI elements will be scaled depending on screen size. I make button bigger holding shift key to scale it proportionally. Put it at the center. Ok. By default all of button states are visualized by color tint. In my case I want button states to be represented by different sprites. So I change transition option to color swap and D&D sprite which is responsible for down state into pressed sprite slot here. Very well. Sounds playback will be controlled by button sounds script. Let’s take a look at it.
Here I have a couple of audioclip variables which are button down sound and button up sound which will be played when button is pressed and released. Serialize field attribute allows me to assign these variables in inspector. Also here I have audio source variable which allows me to control an audio source component which will be attached to button to play those sounds. In start method I take a control over that audio source component. Play down sound method will be invoked when button is pressed down. When it happens button down sound is played. Play up sound method will be invoked when button is released. In this case button up sound is played. Pretty simple. D&D this script to button. Select button and lets give it final tunings. Here is button sounds script attached. D&D corresponding sound clips into their slots. In my case O goes into button down sound slot and OU goes into button up sound slot. Ok. To play sounds button needs an audio source component. So I add one to the button. Here we go. To make it possible to invoke those methods from button sounds script button needs an event trigger component which will detect when I click and release my button. Here it is. This component has to be tuned. Add new event type which will be pointer down. It triggers when button is pressed down. Hit plus sign to create a kind of response to this event. D&D button into this slot and here in function drop down select play down sound method from button sound script attached to our button. Now this method will be invoked when button is pressed down. Add new event type which is pointer up one. This event takes place when button is released. Button is already added so select play up sound from button sounds script.
If you like what I'm doing then you can support me through
patreon.com/alexanderzotov
or here
paypal.me/AZotov
Thank you)
This video is about: unity 2d tutorial, How To Play Different Sounds When You Press And Release UI Button, unity press button, unity button, unity ui button, unity press button sound, unity button sound, unity play sound when button press, unity sound, unity button sound, unity button press sound, unity button release sound, unity button tutorial, unity button animation.










