Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
88 views
in Technique[技术] by (71.8m points)

c# - Why when getting the volume parameter value the volume is so loud?

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class BackToMainMenu : MonoBehaviour
{
    public GameObject[] objsToDisable;
    public AudioMixer audioMixer;

    public static bool gameSceneLoaded;

    private void Awake()
    {
        gameSceneLoaded = true;
    }

    // Start is called before the first frame update
    void Start()
    {
        audioMixer.SetFloat("gamemusicvolume", Mathf.Log10(PlayerPrefs.GetFloat("mainmenumusicvolume")) * 20);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (Time.timeScale == 0)
            {
                DisableEnableUiTexts(true);
                SceneManager.UnloadSceneAsync(0);
                Cursor.visible = false;
                Time.timeScale = 1;
            }
            else
            {
                Time.timeScale = 0;
                MenuController.LoadSceneForSavedGame = false;
                SceneManager.LoadScene(0, LoadSceneMode.Additive);
                SceneManager.sceneLoaded += SceneManager_sceneLoaded;

                Cursor.visible = true;
            }
        }
    }

    private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
    {
        audioMixer.SetFloat("gamemusicvolume", Mathf.Log(0.0001f) * 20);
        DisableEnableUiTexts(false);
    }

    private void DisableEnableUiTexts(bool enabled)
    {
        foreach (GameObject go in objsToDisable)
        {
            if (go.name == "Cameras")
            {
                foreach(Transform child in go.transform)
                {
                    if(child.name == "Main Camera")
                    {
                        if (enabled == false)
                        {
                            child.GetComponent<Camera>().enabled = false;
                        }
                        else
                        {
                            child.GetComponent<Camera>().enabled = true;
                        }
                    }
                }
            }
            else
            {
                go.SetActive(enabled);
            }
        }
    }
}

When running the game the Main Menu scene start then when making a new game the Game scene is loaded and then here in the Start I'm getting the Main Menu volume float parameter and set it to the Game Music volume.

void Start()
        {
            audioMixer.SetFloat("gamemusicvolume", Mathf.Log10(PlayerPrefs.GetFloat("mainmenumusicvolume")) * 20);
        }

When in the Main Menu scene the volume is -4.01 dB of the main menu music.

Main Menu music volume is -4.01 dB

Then when it's getting the volume of the main menu and set it to the Game music volume the Game music volume is 35.99 dB and I can't figure out why it's setting the volume to so high value ?

The game music volume value is 35.99 dB

Could be the calculation to get the volume in the Start is wrong ? It should not be Log10 ? Or not * 20 ?

audioMixer.SetFloat("gamemusicvolume", Mathf.Log10(PlayerPrefs.GetFloat("mainmenumusicvolume")) * 20);

How come it's getting from -4.01 dB to 35.99 dB ?

This script in the Mein Menu scene is setting the music and sfx volumes of the main menu using ui sliders :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
using TMPro;
using System;
using UnityEngine.Events;
using System.Linq;

public class Settings : MonoBehaviour
{
    [SerializeField] private AudioSource[] audioSources;
    public AudioMixer audioMixer;
    public TMP_Dropdown resolutionDropdown;
    public TMP_Dropdown qualityDropdown;
    public Text musicText;
    public Text sfxText;
    public Slider[] audioSliders;
    public Toggle fullScreenToggle;

    private Resolution[] resolutions;

    private void Awake()
    {
        audioSources = GetComponents<AudioSource>();

        resolutionDropdown.onValueChanged.AddListener(new UnityAction<int>(index =>
        {
            PlayerPrefs.SetInt("resolutionvalue", resolutionDropdown.value);
            PlayerPrefs.Save();

        }));

        qualityDropdown.onValueChanged.AddListener(new UnityAction<int>(index =>
        {
            PlayerPrefs.SetInt("qualityvalue", qualityDropdown.value);
            PlayerPrefs.Save();

        }));

        fullScreenToggle.onValueChanged.AddListener(new UnityAction<bool>(index =>
        {
            PlayerPrefs.SetInt("fullscreen", boolToInt(fullScreenToggle.isOn));
            PlayerPrefs.Save();

        }));
    }

    private void Start()
    {
        qualityDropdown.value = PlayerPrefs.GetInt("qualityvalue");

        var resolutions = Screen.resolutions.Where(resolution => resolution.refreshRate == 60).ToArray();
        resolutionDropdown.ClearOptions();

        List<string> options = new List<string>();

        int currentResolutionIndex = 0;
        for(int i = 0; i < resolutions.Length; i++)
        {
            string option = resolutions[i].width + " x " + resolutions[i].height;
            options.Add(option);

            if(resolutions[i].width == Screen.currentResolution.width &&
                resolutions[i].height == Screen.currentResolution.height)
            {
                currentResolutionIndex = i;
            }
        }

        resolutionDropdown.AddOptions(options);
        resolutionDropdown.value = PlayerPrefs.GetInt("resolutionvalue", currentResolutionIndex);
        resolutionDropdown.RefreshShownValue();

        float musicvolume = PlayerPrefs.GetFloat("mainmenumusicvolume");
        float sfxvolume = PlayerPrefs.GetFloat("mainmenusfxvolume");

        musicText.text = musicvolume.ToString();
        sfxText.text = sfxvolume.ToString();
        audioSliders[0].value = musicvolume / 100f;
        audioSliders[1].value = sfxvolume / 100f;

        fullScreenToggle.isOn = intToBool(PlayerPrefs.GetInt("fullscreen", 0));
        
    }

    public void SetResolution(int resolutionIndex)
    {
        if (resolutions != null)
        {
            Resolution resolution = resolutions[resolutionIndex];
            Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);
        }
    }

    public void SetMusicVolume(float volume)
    {
        audioMixer.SetFloat("mainmenumusicvolume", Mathf.Log10(volume) * 20);
        musicText.text = Math.Round(volume * 100, MidpointRounding.AwayFromZero).ToString();

        PlayerPrefs.SetFloat("mainmenumusicvolume", (float)Math.Round(volume * 100, MidpointRounding.AwayFromZero));
    }

    public void SetSfxVolume(float volume)
    {
        audioMixer.SetFloat("mainmenusfxvolume", Mathf.Log10(volume) * 20);
        sfxText.text = Math.Round(volume * 100, MidpointRounding.AwayFromZero).ToString();

        PlayerPrefs.SetFloat("mainmenusfxvolume", (float)Math.Round(volume * 100, MidpointRounding.AwayFromZero));

        if (!audioSources[1].isPlaying)
            audioSources[1].Play();
    }

    public void SetQuality(int qualityIndex)
    {
        QualitySettings.SetQualityLevel(qualityIndex);
    }

    public void SetFullscreen(bool isFullscreen)
    {
        Screen.fullScreen = isFullscreen;        
    }

    int boolToInt(bool val)
    {
        if (val)
            return 1;
        else
            return 0;
    }

    bool intToBool(int val)
    {
        if (val != 0)
            return true;
        else
            return false;
    }
}

The SetMusicVolume is called by the slider event of the main menu music.

question from:https://stackoverflow.com/questions/65838204/why-when-getting-the-volume-parameter-value-the-volume-is-so-loud

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems that you are mixing some of the string parameters.

In your first script you're setting the audio mixer gamemusicvolume parameter like this:

    audioMixer.SetFloat("gamemusicvolume", Mathf.Log10(PlayerPrefs.GetFloat("mainmenumusicvolume")) * 20);

And then in the second script you're setting the audio mixer mainmenumusicvolume parameter instead of the gamemusicvolume parameter.

    audioMixer.SetFloat("mainmenumusicvolume", Mathf.Log10(volume) * 20);

Moreover, later in the first script you set the gamemusicvolume to an even lower value:

    audioMixer.SetFloat("gamemusicvolume", Mathf.Log(0.0001f) * 20);

All in all, this code feels wrong and overcomplicated. Why do you save a value and then do Log * 20 manipulations? Why not just save the actual value that you want, keep your code simple.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...