Search code examples
c#unity-game-engineaudiouislidermixer

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


Solution

  • 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);