From the course: Cert Prep: Unity Certified Associate Game Developer Audio and Effects

Coding with audiomixers - Unity Tutorial

From the course: Cert Prep: Unity Certified Associate Game Developer Audio and Effects

Start my 1-month free trial

Coding with audiomixers

- [Instructor] In this chapter, we're going to look at different ways that we can code alongside the unity audio system. And we'll start by looking at a practical example of some code that will gradually fade in the volume of an audio source so that over time as we play back a scene the volume will fade in. To do this I'm going to revisit our audio mixer that we created previously by moving to the audio mixer window here and inside the audio mixer I'm going to select our master group since this controls the global volume of the group. To start coding with this we need to create an exposed parameter. That is, we need to tell the audio mixer what values we want to be accessible in code. It's really easy to set up. By selecting the master group and moving to the inspector, in the inspector here, I'm going to hover my mouse over the volume field and right click with my mouse. Now, I want to make sure my mouse was over the volume field not the attenuation header, actually the volume field and right-click with my mouse. From the list I'm going to select expose volume of master to script. Now, you can always tell and confirm this has been applied by moving to the volume field and next to it you will see a small arrow pointing to the right. Now, this is not the only thing you need to do, just move down here back to the audio mixer, to the top right-hand side, to the exposed parameters drop-down. Click on that drop-down and inside the exposed prominence dropdown here you can see that it selected the parameter, the volume parameter. By default it has the name of my exposed param, which isn't descriptive at all. But you can change that, just right-click, choose rename and you can give this any name you want. I'm going to choose the capitalized version, Volume, that's Volume with a capital V here, press Enter on the keyboard to assign that parameter the name of Volume. Now, to start coding with this, I'm going to go back to the project panel and I'm going to move to the scripts folder where there are already some scripts but we're going to be adding a completely new one. I'm going to right-click inside here, choose create and then select C-sharp script. And I'm going to call this volume fader and press Enter on the keyboard to accept those changes. Now we need to apply this to an object in the scene. So I'll create a completely new and empty object to do that. I'll choose game object, create empty and I'm going to give this the name of volume fader presents from the keyboard and recenter this back at the wild origin here and then drag and drop the volume fader script onto the volume fader object. Now that script has been applied, I can open that inside visual studio here just by double clicking the volume fader script that will bring the code here into visual studio. And I'll just resize that to the recording window to take a look at our script here. Now, the first thing that we need to do if we're going to be coding with audio mixers and we are is to include a new namespace right up top here. So I'll add a new line and select using UnityEngine.Audio and I make sure I add that namespace and then choose a file and then choose save. The next thing that I want to do as part of this class here inside the volume fader is to add a new public variable where I can expose the audio mixer and specify that as the asset I'm going to be working with. To do that I create a new variable public AudioMixer and I'm simply going to call this mixer and it's going to equal the initial value of null. Here inside the start function, all the update function we're going to have the ability to tweak the value of the audio mixer. But first, let's get that set up, I'm going to choose file and then save to save the code I've created and go back to unity, unity will compile that if it needs to. Select the volume fader objects and you can see here on the volume fader we have the volume fader script and right at the top here, we have the mix of field asking us to select the mixer that we're going to be working with. We can easily do that by moving to our assets folder dragging and dropping the mixer that we created in the previous chapter here into the mix of field, and there we have specified our mixer. That's great, this is now ready to get set up to code with it. In the next movie, we're actually going to create the function itself, that's going to use this script file to fade in the volume in our scene.

Contents