如何在Unity中将混音器的音量设置为滑块的音量? [英] How to set a Mixer's volume to a slider's volume in Unity?

查看:91
本文介绍了如何在Unity中将混音器的音量设置为滑块的音量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行一些音频设置.这是我的脚本:

I'm trying to make some audio settings. Here is my script:

public AudioMixer masterMixer;
public float masterLvl;
public float musicLvl;
public float sfxLvl;

public void SetMasterVolume ()
{
    masterLvl = masterVolumeSlider.value;
    masterMixer.SetFloat("masterVol", masterLvl);
}

public void SetMusicVolume()
{
    musicLvl = musicVolumeSlider.value;
    masterMixer.SetFloat("musicVol", musicLvl);
}

public void SetSfxVolume()
{
    sfxLvl = sfxVolumeSlider.value;
    masterMixer.SetFloat("sfxVol", sfxLvl);
}

它具有所有OnValueChanged();滑块上的东西.我只想知道为什么这行不通.谢谢.

It has all the OnValueChanged(); things on the sliders. I just want to know why this doesn't work. Thanks.

所以事情是,它改变了 dB ,而不是音量.新问题是:如何使其改变音量而不是dB​​?

So the thing is that it changes the dB, not the volume. The new question is: How do I make it change the volume instead of dB?

屏幕截图.

推荐答案

我认为,您的问题是混频器值(-80db-20db)是对数刻度,滑块值是线性的.例如:一半的音量实际上约为-10db,但是如果将其连接到线性标尺(如滑块),则一半的音量最终将为-40db!这就是为什么在那一刻听起来基本上是沉默的原因.

I think, your problem is that the mixer value (-80db - 20db) is a logarithmic scale and the slider value is linear. For example: half volume is actually about -10db, but if you connect it to a linear scale, like the slider, then half volume will end up being -40db! Which is why it sounds like it's basically silent at that point.

有一个简单的解决方法:

There's an easy way to fix it:

  1. 而不是将滑块的最小/最大值设置为-80和20,而是将其设置为最小0.0001和最大1.

  1. Instead of setting the slider min / max values to -80 and 20, set them to min 0.0001 and max 1.

在脚本中设置暴露参数的值,使用它可以将线性值转换为衰减水平:

In the script to set the value of the exposed parameter, use this to convert the linear value to an attenuation level:

 masterMixer.SetFloat("musicVol", Mathf.Log10(masterLevel) * 20);

将最小值设置为0.0001非常重要,否则将其降至零将破坏计算并再次增大音量.

It's important to set the min value to 0.0001, otherwise dropping it all the way to zero breaks the calculation and puts the volume up again.

帖子:

https://forum.unity.com/threads/changing-audio-mixer-group-volume-with-ui-slider.297884/

这篇关于如何在Unity中将混音器的音量设置为滑块的音量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆