public class SoundTest : MonoBehaviour
{
[SerializeField] private Slider _slider = null;
[SerializeField] private AudioMixer _mixer = null;
[SerializeField] private float _minVolume = -40;
[SerializeField] private float _maxVolume = 0;
private const string Ambient = nameof(Ambient);
private void OnEnable()
{
_slider.onValueChanged.AddListener(ChangeVolume);
}
private void OnDisable()
{
_slider.onValueChanged.RemoveListener(ChangeVolume);
}
private void ChangeVolume(float volume)
{
volume = Mathf.Lerp(_minVolume, _maxVolume, volume);
SetAmbientVolume(volume);
}
private void SetAmbientVolume(float volume)
{
if (volume <= _minVolume)
{
_mixer.SetFloat(Ambient, -80);
return;
}
_mixer.SetFloat(Ambient, volume);
}
}