Hello dear friends. İn this video we will learn how to control audio volume with slider. Actually we're going to learn how to control audio mixer with slider. And how to save volume settings? So when we restart the game, the previous volume settings will be kept. Ok lets get started. In the previous video, we learned how to use Audio Mixer. Today we will create a slider and control the audio mixer with this slider. I will create a script called Volume Settings to provide the connection between the slider and the audio mixer. Let's create a slider first. Right click on the Hierarchy panel. Click on UI and Slider. As you can see, when I add a Slider, the canvas and the EventSystem are added automatically. I'm gonna share to detailed videos about Canvas and UI systems. You can subscribe so you don't miss it. I’m gonna change the size of the slider. And you can customize the slider as you want. I want to add a text to the slider. Right click on the slider. Go to UI and click on Text. Set the position of the slider. Start the game. The slider is ready. But as you can see the slider doesn't change the volume of the music. Let's connection the
slider to the audio mixer. Select canvas. And I’m gonna create a
script named Volume Settings. Open the script. I'm going to add audio library and UI library. I created audio mixer variable named myMixer to access the audio mixer. I'm going to create slider variable named music slider to access the Volume Slider. Save the script and go back to uniy. Drag and drop the music slider to here. And Drag and drop the Audio Mixer to here. Select music group. To change the volume of the music, we have to change the volume parameter. So we need to access the volume parameter from the Volume Settings script. To access a parameter of the audio mixer we have to expose it. Right click on the volume parameter and expose it. And you can see the
exposed parameters here. I'm going to rename it to music. using this name we will access this parameter from the script. Go back to the script. I'm going to create a method
named "set music volume" as public. First get the slider value. Then set volume parameter value of audio mixer to the slider value. Save and go back to unity. Select music slider then click the plus button. Drag and drop canvas object here, because it has Volume Settings script. Select volume settings script then select SetMusicVolume Method. SetMusicVolume function will work as the slider moves. Ok start the game and test it. As you can see, we can control the audio mixer with the slider. But it only moves between 0 and 1. Because the slider value is between 0 and 1. Audio mixer volume changes logarithmically. But the value of the slider changes linearly. I will get the logarithmic value of the slider and I'll multiply it by 20. Go back to unity. I'm going to change the minimum value of the slider to 0.0001. Ok! start the game again. Nice! As you can see, we can now control the music properly with the slider. But when the game first starts the audio mixer is not
compatible with Slider. After moving the slider its becomes compatible. I'm gonna run the SetMusicVolume function when the game starts. So the slider and the audio
mixer will be compatible. Start the game. As you can see, when the game starts, the slider and the audio mixer are compatible with each other. I turn the volume of the music to the highest level and exit the game. And when we restart the game, the music level is not
at the highest level. Let's see how to save the music volume settings. I'm gonna use PlayerPrefs Class to store player preferences. We can store string, float and integer values by using PlayerPrefs Class. I will store the value of the slider with the musicVolume key name. I’m gonna create a new method named LoadVolume. i will equate the slider's value to the value i have stored with the musicVolume
key name in this method. Then I will call SetMusicVolume method. When the game starts If a data named musicVolume
key has been stored , I'm gonna run the LoadVolume method. if not I'm gonna run SetMusicVolume Method. Ok! Save and back unity. Start the game. I will turn off the music and exit the game. Restart the game. As you can see the music is muted. If you leave the music volume at which level, when you start the game again, the music level will remain at that level. Let's quickly create a Slider for SFX too. Duplicate the Music Slider and rename it to SFX slider. I will follow the same
steps with the music slider. As you can see SFX Slider is working properly. You can control the volume of music and sound effects separately. Let's check the save system. Restart the game. Nice!. I hope the video useful for you. Thanks for watching. See you in the next video.