AUDIO MIXERS In Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys and in this video i'm going to show you how to use audio mixers adjust its values with sliders and save the sliders values between scenes so let's jump right into it so first of all we need some sounds to control with the audio mixer so i'm going to create some bubble sound effects go ahead and create an empty object and i'm going to call it audio manager and reset its transform this audio manager is going to have an audio source and i want this audio source to play my bubbles clip and we're gonna make sure it's looping so now if we hit play we have sound so now with sound let's create an audio mixer right click create and go audio mixer i'm gonna call this mixer go ahead and double tap your mixer and it'll bring up this window you can undock it if you want but i like to keep it docked down here and the first thing that you'll notice is in our groups we have this master channel and if we hit play we can press edit and play mode and if we adjust this it doesn't change our bubbles volume that's because we need to go to our audio source and in the output select our master channel hit play and now we can control the volume but i want this to be a little bit more flexible so instead i'm going to create separate channels so i'm going to create one for music and press on master again add another channel and i'm going to call this one sound effects so now i'm going to go back to the audio manager audio source and instead of the master channel i'm going to select the music channel i know it's bubbles but let's just pretend it's music so now with that selected i also have button sound effects so i'm going to make sure to go to my button sound effects master and make sure the output is under the sound effects channel and also how the channeling works for all these groups is the parent controls the children's volume so if i child music to sound effects and if we hit play you could see that the sound effects channel now has control over the music now let's undo that because i don't want that and now i want to explain the solo mute and bypass buttons so if we hit play if we press solo on the sound effects you'll only hear the sound effects but if we hit mute on one of them it mutes them and what bypass does is if you add an attenuation to your channel so if i hit edit and play mode and add an attenuation and let's select an echo and in the inspector i could just play with its values say 18 and 47. we could see that the bubble sounds completely different but if we hit bypass it bypasses all of the effects that's all i want to cover on the mixer now let's create sliders that the player can use to control these channels so let's go ahead and create a ui canvas and do not delete your event system i already have one on my tank canvas and on your canvas right click and create a slider jump into the scene view i'm just going gonna make this a lot bigger and i'm gonna slide this to the right i'm gonna rename this to music slider and i'm gonna take the fill out of the fill area and delete it and take the handle out of the handle slide area and delete it because those objects are useless now i'm just going to customize my slider a little bit i'm going to make the fill some kind of red and that looks alright and now let's label this slider so right click on your slider and go ui text mesh pro i'll rename this to text i'll go in its settings and align it on both axes and i'll go into the scene view hit w slide it up and i'll change the text to music and change its font that looks pretty good now let's just duplicate this slider and call this sound effect slider go into the scene view and slide it down and i'll change the text to sound effects go to the game view that looks pretty good now we need to connect these sliders to the mixer channels the way that we're going to do that is we're going to create a new c-sharp script i'm going to call this volume settings now i'm calling it volume settings because you're probably going to have these sliders in your settings menu so this is an appropriate name so now i'm going to select my canvas and drag it onto it just like that open it up now we're not going to need these two namespaces for co-routines or lists and we're not going to use these right now let's go ahead and create a serialized field for our audio mixer and call it mixer and you can see that we don't have access to the audio mixer that's because we need to be using unity engine dot audio in order to access that and i'm also going to create a serialized field for our slider but we need to be using unity engine dot user interface i'm going to call this music slider and i'm going to duplicate this for sound effects slider now save your class jump into unity and let's assign those right away assign your mixer your music slider and your sound effect slider back to visual studio now let's create a awake function i don't use the private keyword because it's useless void and private are the same so in here we're gonna go music slider dot on value changed so whenever the player changes the value of the slider on value changed is called and we're going to go on value change.add a listener so add a function to that and this function requires a float parameter so we're going to create a void set music volume and it's going to require the float parameter and we'll call it value drop the brackets and we'll pass this in here set music volume and if you have any other parameters it won't work now in this function we want to go mixer dot set float and we want to set the float of our music channel and to do that we have to tap on our music channel and go to volume right click and expose this parameter and down here open it up and you'll see this parameter i'm going to rename that to music volume i'm going to copy this so that we don't misspell it jump back into visual studio and create a constant string call it mixer music it's going to be equal to quotations and then paste in the music volume i'm going to copy this constant string and paste it and call it mixer sound effects back to unity go to the sound effects channel go to the volume expose it and go to this parameter and let's call it sound effects volume and copy it back into visual studio let's paste that here for sure we know we didn't misspell it and now in the set flow we're going to set the float for the mixer music channel and now we need a value and the value we're gonna give it is the value passed in from the slider now let's just go test that out and if we adjust our music slider you could see that it's only changing the channel's volume by one decibel that is because the slider's value is linear but the channel's value is logarithmic the linear scale goes by increments of 1 and -1 whereas the logarithmic scale goes by increments of times 10 and decreases by dividing by 10. so it goes to 1 to 10 to 100 but when you decrease it it goes from 1 to 0.1 to 0.01 and so on so forth they do this because it's easier to graph so we have to convert this value to the logarithmic value and the way that we do that is using math dot log 10 and we'll log the value and we multiply this value by 20 so that we're able to reach minus 90 decibels which is zero volume save hit play and now this is working much better but you'll see if we hit zero it goes back to a hundred percent that's because you're supposed to set your slider's minimum value to 0.0001 because if it hits 0 it'll just break the channel and now it's working now let's hook up our sound effect slider as well so we'll just copy this line and paste it here be very careful when you're copying paste in code because you can easily forget to say maybe you forget to set this to sound effects slider but you do change the function then the sound effect slider will never work so be extra cautious but i'm doing that in this tutorial to save some time go ahead and copy and paste this function down here we'll call this set sound effects volume we want to use the mixer sound effects string now we can call that for the sound effects slider instead save that up jump into unity now if we set the sound effects to zero you won't be able to hear button sound effects it works now we want to be able to save these values so the player doesn't have to set them every single time they play and we'll save them between scenes as well so our canvas will be doing the saving and our audio manager will be loading the channel's volumes let's go ahead and create a c-sharp script called audio manager select our audio manager and drag it onto it open it up and i won't use this namespace or start an update so the first thing that we need to do is create a singleton pretty simple let's create a public static audio manager called instance and create an awake function and we check if instance is equal to null meaning it hasn't been set and it doesn't exist we say instance is equal to this and now we don't destroy this game object on load we have to pass in the game object so that all of our components and children prevail with us otherwise if there's already an instance in the scene we'll just destroy this game object so that there's not two in a given scene i actually forgot to tell you that you need to prefab your audio manager and go to all of your other scenes so on tank 2 wherever you need the audio manager just drag the prefab in there so that if you start at tank 2 you'll have an audio manager in there and i'll do that for all of the scenes and that's it singleton created now this is public static so it can now be accessed from anywhere you'll probably have other classes needing a reference to it anyways for instance i just created this eat sound effects function so now i'll have my fish go audiomanager.instance.edu sound effects so it'll call this function because it is public and because this instance is public static so it's a really nifty way to get access to this class now you see that i coded this immediately i already thoroughly explained how to do this in another video all i did was got a reference to an e audio source had a list of e audio clips and i play a random clip one shot and you could see that i have a reference to my child which has an audio source on it and make sure this heat source output is connected to the mixer sound effects channel and a reference to three fish eat clips this isn't relevant to audio mixers but if you want a more thorough explanation you could watch this video now jumping back into audio manager like i said this class will only be doing the loading and the volume settings will be doing the saving so in here we're going to create a constant string and this will be the music key it's going to be equal to music volume and i'll copy this and paste it for the sound effects key and change its string so we need the key in order to show player props where to save it so before we create the load function let's make the save function so go into volume settings and on disable so when you exit the scene or the object is disabled we'll go ahead and save here we'll go player press dot set float so we're saving and now we need a key and that key is an audio manager so we'll make these constant strings public so that we can access them jump back in here and go audio manager dot music key and the value that we want to save is music slider dot value now we'll copy and paste this for the sound effects slider use the sound effects key and the sound effects slider dot value now with the values being saved let's go ahead in our audio manager and let's create a void load volume we're putting the load function in here because the audio manager is going to be in every scene but the volume settings won't be in every scene so we can't load off of this because this is only in the settings so this solves the problem but it also makes it a little complicated to clear that up a little bit let's add a comment saying volume saved in volume settings dot cs now to load the volume we need access to the mixer so we'll go up there and serialize a field audio mixer and we need to be using unity engine dot audio and we'll call it mixer don't wait save your class go assign that right now back into visual studio now we can go mixer dot set float and we're going to set the float of the channel's name and the channel names are in here we need to make these constant strings public so that we can access them back into here and now we're going to go volume settings dot mixer music and the value we want to set it with is the loaded value so to keep it more clear i'll just create a float called music volume it's going to be equal to playerprefs.getflow so we're getting it this time or loading it and again we need this key and the key is music key and if we fail to load the key if it doesn't exist then we'll have to pass in a default value and we'll set that to 1f so 100 volume we'll copy and paste this for the sound effects volume using the sound effects key now keep in mind that this was saved in the slider's linear value so it's going from 0 to 1 but the mixer's channel uses a logarithmic value so again we'll have to use math.log 10 to log this music volume and again we'll multiply it by 20. and we'll duplicate this for the mixer sound effects channel and we'll pass in the sound effects volume now i'm just going to make sure you don't make this mistake for some reason if you math log this directly in this variable instead of math logging it here it won't work in other scenes so just make sure to do it this way now with the function created we need to actually call it and we'll do that on awake so load volume and it will load our volume and now if the volume settings are ever enabled we'll just make sure the sliders update their values to whatever was loaded so on start we'll say music slider dot value see we're setting the value again is equal to now we have to load the value again player press dot get float and we need these keys so audio manager dot music key remember default value if it's not found will be one f and we'll copy and paste this for the sound effects slider using the sound effects key now that was a bit daunting i kind of just did everything at once sorry if i'm going a bit fast this video might be too long but if we hit play now now it's not going to load any value because we didn't save anything yet and you could see that since a save wasn't found it set the slider's values to the default value which was 1. and if we set sound effects to 0 and music to about 75 and if we jump into the next scene keeping our eye on these decibel levels we'll see that they persist between the scenes awesome now i could have literally saved with four lines of code but it wouldn't persist between loads i want this to work for everybody so this should be everything you need to know about audio mixers and even how to save their values if this video helped you out be sure to like and subscribe so i can make more content like this if you have any suggestions leave it in the comments and with that being said i'll see you guys next time
Info
Channel: Kap Koder
Views: 405
Rating: undefined out of 5
Keywords: audio, mixers, unity, sound, sliders, decibels, sfx, save, loads
Id: pbuJUaO-wpY
Channel Id: undefined
Length: 16min 45sec (1005 seconds)
Published: Tue Oct 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.