C# script to controll sound volume level using UI Slider.
Simply, create UI Slider with minimal value 0 and maximum value 1. After that attach below script to it.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; // ? 2017 TheFlyingKeyboard and released under MIT License // theflyingkeyboard.net public class VolumeSlider : MonoBehaviour { [SerializeField] private Slider slider; void Start() { if (!PlayerPrefs.HasKey("SoundVolumeLevel")) { PlayerPrefs.SetFloat("SoundVolumeLevel", 1.0f); } else { slider.value = PlayerPrefs.GetFloat("SoundVolumeLevel"); ChangeValue(); } } public void ChangeValue() { AudioListener.volume = slider.value; PlayerPrefs.SetFloat("SoundVolumeLevel", slider.value); } }
Unity C# UI Sound Level Controller