Hello guys.
Welcome to the video.
So I did double check regarding the background music and it's played one after another, so it should
be fine.
We have no reason to worry about that.
And now let's set up the settings we have in the game.
So I'm going to just make a script.
In the scripts UI, we're going to need UI settings.
UI settings.
All right.
Let's go and find it.
And in this script, we're going to set up, uh, volume according to the slider value we have in the
UI.
Uh, but before we do that, we need to go to the audio and we're going to need to make audio mixer
that is done through create audio audio mixer.
Let's call it audio mixer.
Just like that.
And then we need to go to window audio.
Audio mixer.
Okay.
And there is a window for audio mixer.
With the help of this mixer we can adjust volume of different sound groups.
We just need to select it over here.
Then we need to go to groups and we need to hit plus.
And one of them will be background music.
Then we need to hit plus again.
Uh, but not as a child.
I'm sorry.
We need to select master and add sound effects.
So now we have two mixers that can control volume of the sound.
And we're going to just pass the value from a slider into this mixer.
And thanks to that it's going to adjust sound volume.
Uh, but before we do that.
We need to open the.
Uh, background music we have.
And for the output, we need to choose background music.
Then we need to take all of the sound effects.
And for the output, we need to choose sound effects.
So even now with the setup here we can go to play mode.
And we can increase.
Sorry, we can increase volume of the background music.
Just with help of this slider.
We only need to allow edit and play mode.
Okay, so let me just try that out.
And you can see it works.
Now the goal is to pass value from the slider onto mixer.
For that, we're going to need to expose parameters of this mixer over here.
So we're going to click here.
And actually we can do that by clicking here.
Wait where is the button I yeah I'm sure there is a button over here.
Yeah.
Over here.
We need to do expose volume of background music to script.
Nice.
Now we have a parameter.
We're going to just rename it to BGM.
Then we're going to select Sound effect.
Click right button expose volume of sound effect to script.
Let's find it and let's change it to SFX.
Perfect.
Very nice.
Now let's go to Canvas Settings.
And over here we need to add component UI settings that we have.
After that we need to do something very simple.
We need to go to the script.
And above we're going to make a header under.
Sorry.
So fixed settings.
And under this header we're going to do serialized field.
Private slider is going to be reference to the component of a slider.
So we can get value from there.
Let's call it a suffixed slider.
Then we're going to need serialized field private text mesh pro GUI a suffix slider text.
Because I want to display the percentage of the value to the player so he can understand what the volume
level he has.
And then we need serialized field private string as a fix parameter.
And that is the parameter we just exposed in the BGM.
Now we need to have a header for another one BGM settings.
So I'm going to do Serialized field, Private slider, BGM slider, then Serialized Field Private Text
Mesh Pro.
Guy Kkae-geum slider.
Text and sterilize field.
Private string BGM parameter noise.
Uh.
Almost ready.
We just need a reference to the audio mixer itself.
So I'm going to do serialize field private audio mixer.
Audio mixer.
Nice.
Now, before we go further and assign these parameters, we can just make a method to make it all work
like all together.
Right?
Let's make a public void a suffix slider value.
And we need to pass a float value here.
So in this case the value will be passed when we move the slider in the UI.
That's what we're going to do.
And then over here we're going to do audio mixer set float of as a fixed parameter to the value we pass
in.
And let's go ahead and just test this with one slider because there is an issue that we need to fix.
And before we fix that I want to show you what's there.
Like the problem is you know.
Yeah.
So let's go ahead and find audio mixer for this one.
Then we need to open settings.
BGM volume and SFX volume, and we need to assign slider over here just so we don't see them with the
same name.
I'm going to rename this to slider BGM and this will be slider suffix.
All right, back to settings.
Let's drag slider of Sound Effects and slider of background music.
For the text.
We did not make the object yet, so let's wait for it.
And then for the parameter we need to type suffix bgm.
Okay, I'm going to switch off main menu and I'm going to enable settings.
And yeah, let's just go ahead and see this.
Let's go to play mode.
You know what, it would be better to test this with a background, honestly.
So I'm going to make a public void BGM slider value.
Float value.
Then, uh, sorry.
Audio mixer set float of BGM parameter to the value.
Okay, let's save this and let's go and try this with the background music because background music
is always playing.
So it's easier that way.
Okay, so you can see I'm dragging the slider.
And wait.
It does not work at all.
Give me a second why it's not working?
Because I forgot to do something here.
We need to go here.
Hit plus, drag settings over here and find UI settings.
And we need to choose BGM slider value from the above over here.
All right.
And for the sound effects going to be the same let's drag settings and choose SFX Slider value.
All right.
Now it's going to work.
Let's go to play mode.
And you can see I'm dragging the slider.
It works.
It does affect the mixer over here.
Yeah, sorry.
So it does work and it does affect mixer over here.
But the problem is it moves it only by one because BGM slider is from 0 to 1.
And that's the issue.
We need to somehow apply it here so it will be affected all the way down or all the way above.
Because mixer itself has plus 20 -80.
You could technically just set values over here -80 to 20, but that is not really a good way to do
that.
So we're going to go and make a multiplier for that.
That will make it easier for us.
Um, I'm going to make a serialized field.
Private float.
I'm going to do mixer multiplier equals to 25 by default.
Then over here we're going to make float new value.
He goes to math F.
Log ten of value and we're going to multiply it by mixer multiplier.
Just like that.
And then over here we're going to use new value.
Same goes here.
We need to do it here I'm going to do float new value.
Uh I guess I can just duplicate this line and move it here.
And then we're going to pass new value here because of that.
Now the slider will move much more.
I would say okay, let's go ahead and try it.
And now you can see I'm adjusting the slider and it goes way down like that.
There is a bug.
We can completely reduce the volume and it will snap back to zero.
That happens because while you here can be zero, and when you multiply something by zero, it becomes
zero.
So to fix that, we need to select these both sliders and make minimum value as 0.01.
Now it will never go to zero.
If you make it minimum it's going to be minimum.
Just like that.
And now you can hear the music and you cannot hear the music.
All right.
This is very good.
Now let's add the percentage so you can see percent.
And we can just, you know just a bit of a polish here.
And let's go to the slider.
We're going to find the handle area.
There is the handle.
And I'm going to give it a child of a text mesh Pro.
I'm going to just call it percentage suffix.
And let's imagine this one says 46%, just that we're going to align it to the center.
I guess we probably should change the font to what we usually have.
And now we're going to move it below like that.
Just like that.
Very cool.
Now let's duplicate it.
Let's move it to.
Let's make it as a child of a handle over here.
We're going to change it to BGM.
We're going to.
Line it in the center and then move it down like that as well.
We can't really see it because of the character here, but I think it's okay.
It doesn't matter.
You can just move character or you can raise up the entire slider like that.
All right, not a problem.
Cool.
So now we have this text.
Let's go to settings.
And let's assign this text by just dragging these two percentage BGM and this one to as a fix okay nice
I like it.
What I don't like though is that we have a suffix BGM.
We have a suffix BGM, but in the hierarchy it's the opposite.
I hate it, so I'm going to just place it above like that.
All right.
Cool.
So we did assign text over here.
We can go back and we can do something like this.
We can do a suffix slider.
Text dot text equals to mathbf.
We're going to round up the number we have.
So I'm going to do round to int.
And we're going to give it value.
And we're going to multiply it by 100 because slider's value can be from 0 to 1.
And if you multiply it by 100 it's going to be like person like you know, 100 or 1.
So I'm going to do plus here percentage sign.
Thanks to that we're going to have a nice text.
We only need to go here paste it in and replace it with the background slider text.
Okay, we almost done here?
It is almost perfect.
We only need to save the settings every time we close them up, or every time we open the settings.
We need to load up the settings.
Because as of now, even if you change something and you can see new percentage value, it does not
affect your game when you go further, like when you exit the game and play the game again, you would
have to change settings again.
That's not what we need and because of that, we're going to go here and we're going to make on disable
method on disable is called every time you disable the game object.
So this is just the perfect option for us.
We're going to take Playerprefs set float.
And we just can use as a fixed parameter easy for us and give it as a fixed slider value.
Then same goes for Playerprefs set float of BGM parameter and we're going to give it BGM slider value
right?
This is very good.
Also, we're gonna load up the settings.
But for some reason when I did test this on, enable did not work very well.
I'm not sure why, I'm just going to try it now if it will not work.
I have a way around of that.
Okay, so let me copy these two.
Paste them in here.
And then I'm going to do a suffixed slider dot value equals to playerprefs get float of a suffixed parameter,
and by default I want to give it maybe 0.7 f.
So it's not very loud, just 70% of that.
Then we're going to do BGM slider dot value equals to playerprefs.
Getfloat.
And again I'm going to do 0.7 by default if you don't have anything set before.
So I'm going to save this.
I'm going to double check.
But again for some reason on enable did not work last time.
Let me try.
Oh, it works okay.
Then we have even less work to do.
Back settings.
Cool.
Now let me just mute the game.
Going to go to play mode.
If I'm not mistaken.
It's supposed to be like 19 and yes.
All right.
This is just amazing man I love it so much.
All right, settings are done.
Now we can go to the next video.
I'll see you there.