grouptout
grouptout

Reputation: 46

How to set a slider's volume from the audio mixers's volume in Unity?

I have audioMixerGroup.audioMixer.GetFloat("AllVolume", out tmp); tmp will have volume in db. I vant to convert db (-80, 0) to slider's value (0, 1).

in short, I need to do How to set a Mixer's volume to a slider's volume in Unity? just the other way around

Upvotes: 2

Views: 2146

Answers (3)

Rob
Rob

Reputation: 107

I would do the below when you need to use the tmp variable:

(tmp + 80f) * 0.01f

Upvotes: -1

leonardo david
leonardo david

Reputation: 11

When setting the volume you will utilize the following code:

Mathf.Log10(value) *20;

What you need is the inverse:

Mathf.Pow(10, (value/20);

Upvotes: 1

MysticalUser
MysticalUser

Reputation: 414

You can create a Remap function:

float Remap(float value, float min1, float max1, float min2, float max2) 
{
    return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
}

Implementation:

audioMixerGroup.audioMixer.GetFloat("AllVolume", out tmp);
slider.value = Remap(tmp, -80f, 0f, 0f, 1f);

Upvotes: 3

Related Questions