Hello, guys.
In this video, I want to make settings of the UI work.
We're going to make an audio mixer that controls volume of sound effects and background music.
And then we're going to make that sliders in the settings, going to control that parameters.
And regarding those buttons of the day night system, we're going to do it later.
Once we have that system in the game, don't think about these three buttons now.
Let's just take care of these two sliders over here.
So what I'm going to do is go to window.
Audio audio mixer and now I have mixer in the game.
I want to drag it over here to the top.
And if we hit plus over here.
We can create audio mixer.
Now, under this audio mixer, we can create groups for different sounds.
We can do that by clicking plus over here.
And first on alcohol.
Background.
Music.
Now let's select, muster and click one more.
Plus, there's going to be sound effects.
All right.
So with the help of these two sliders, we can control sound effects and background volume.
Let's choose sound effects.
Click output and choose sound effects here.
And same for the background music.
And for background music, we're going to choose output.
Background music.
Now I want to go to play mode and show you how does it work.
We have music playing in the game.
Now I want to show you how does it work?
I'm going to click edit in the play mode.
And if I drag the slider, you can hear my volume of the background.
Music is lower if I drag it into the maximum, it was much louder.
Right?
And we're going to control these two sliders.
Let me set it back to zero.
Yeah, we're going to control these two sliders with the help of sliders in the settings.
Where is it?
Here.
So basically, I'm going to take this value of this slider and pass it to the slider of the mixer.
All right.
Let's create a script first.
It's going to be pretty simple Script UI.
Uh, volume.
Slider.
Let's create serialized field.
First we need a slider.
So private slider.
And I'm going to call it just slider.
Then serialize field private audio mixer and I'm going to call it just audio mixer.
Now if you remember, we had different groups, one for background music, one for sound effects.
So we need kind of a parameter that you're going to access.
So I'm going to do serialize field, private string.
Ideal parameter.
Now let's go below that and create private void on value changed.
We're going to pass a float value and inside we're going to change value of the mixer with the certain
parameter.
So we're going to do an audio mixer.
Said float and then said, You can see we need a name.
So our parameter and as a value, we're going to just pass value, then we're going to make an awake
function.
And we did something like this with the buttons before just now, instead of button, it is a slider,
so we're going to do slider on value changed add listener and we're going to pass function on value
changed.
You know, I kind of don't like to have same name.
So let's change this to slider value.
Slider value.
This looks better.
Now, before we continue, I want to make sure all of this is working.
So let's go back to Unity.
You're going to take slider of the we're going to take slider of the sound track the script.
We're going to.
A sign slider itself.
We're going to assign our mixer, we can just click over here and it's going to be there and we need
our parameter.
How do we make the parameter?
Very simple.
We're going to go to our audio mixer and this one for sounds, right?
So let's choose Sound Effect.
Then we're going to go here to volume, click right button and choose expose volume.
Then here we can click my expose parameter, click twice for change a name, and we're going to change
it to just as a fix.
Now let's do the same for background music.
While we're here, you're going to select background music, expose volume, change the name.
BGM.
Now we know names of those.
We can go back to Slider and type here just as a fix.
Now let's set up background music slider as well.
We're going to drag the script from the script folder UI.
Here in this volume slider lets assign slider itself order mixer and parameter BGM.
Good enough.
Now let's go to play mode and see how does it work?
Look, I'm going to settings and I'm going to select Slider so I can see it.
Here it is for the music.
I can drag this one.
I can see my value getting changed in the inspector.
But the problem is the values in the background music slider is really high, like there is values from
-80 up to 20.
And if I drag this one, it does change this only from 0 to 1.
So we need to make a multiplier.
Sorry, maybe music was too loud so we need to make a multiplier for that.
So while you're going to be changed accordingly.
Let's go and do that.
It's not going to be very difficult.
We're just going to make serialize field, private, float multiplier.
And I want to set default value to something like 25.
Now inside instead of value.
I want to type math f.
Dot log ten and now we can give it a parameter value multiplied by multiplier.
And I think I forgot the parentheses.
Yeah, like that.
Now, while we are here, let's make sure we saving that value.
Once we exit in the settings, it's going to be pretty easy task.
We're going to use on disabled function, which is called when you disable and object on disable.
And here we're going to do player press set float of our parameter to.
Slider dot value.
Pretty simple.
If you want to upload this value, it's going to be easy as well.
We're just going to do slider value.
Equals to their preps.
Get float of audio parameter.
And just in case nothing is saved, we can pass it.
A default value and default value will be the one we set in the inspector Just slider value itself.
Let's save it all and go back to unity.
I'm going to choose my tool sliders and give them default value of point six.
Point six.
Now we can see we go to settings and our slider is at 0.6.
If I drag it to the minimum, the volume is much lower.
If I drag it to the maximum, volume is just like we need.
There is a couple of more issues we need to fix.
The first one, if I drag my slider all the way to the minimum, it's going to snap to default value.
You can see even the slider is at minimum the value is zero now because actual value is zero.
And then we pass in zero over here.
What we can do is to make a slight fix.
Back in the code, we just want to set minimum value of the slider and that is pretty easy.
Slider mean value equals 2.0 01f.
Yeah, we done here.
There is last issue.
As I told you, there are still issues we need to fix.
And this is the last one at the moment we upload in values of the slider in the volume slider away function,
but this function is not called until you switch to settings itself.
So now in the main menu.
Yeah, you can see while you are at zero and if I click settings, then they're going to be changed
to the one we used last time.
So what I want to do here is to load this value from the UI main script.
How are we going to do that?
Pretty simple, actually.
We're just going to wrap this up in the function, so let's select all of three lines, Click alt,
enter, extract method.
It's going to be named as set top slider.
I don't need to work now.
I can delete it.
And I want to make this function public.
Now you I mean, we're going back to you, I mean, and creating a header.
Volume sliders.
Let's create an array of the volume slider script.
So serialize field, private UI volume, slider and array.
Slider.
It's like there is so.
Yeah.
And in the start function, I want to make a short for loop.
We're going to do it as long as we have sliders.
So sliders dot lengths and inside below the sliders.
When I say sliders, I think of the whole small cheeseburgers or something like that.
Really funny.
Let me.
Let me change this to a slider.
I'm sorry.
Sorry.
It took me some time to come down, So we need to pass an eye here.
Slider, I.
Dot.
Set up slide there.
Very good.
Now let's go back to Unity.
Let's select our canvas and drag sliders to slider array.
One, two.
We have it there.
All right.
Now we can see that mixer was set at the beginning, even in the main menu before we clicked settings.
We can change them.
Close it.
Exit the play mode and once we enter it again.
Information is saved.
Perfect.
Let's jump to next video.