简体   繁体   English

如何控制在主菜单中所做的音频更改也应用于游戏场景音频设置?

[英]How can I control the changes of the audio made in main menu to be applied also to the in game scene audio settings?

I have two scenes Game and Main Menu.我有两个场景游戏和主菜单。 I have two sliders in the Main Menu I can control the music volume and the sfx volume and save the changes and it's working fine.我在主菜单中有两个滑块,我可以控制音乐音量和 sfx 音量并保存更改,它工作正常。

The main menu have his own audio mixer for that and the game scene have his own audio mixer.主菜单有他自己的混音器,游戏场景有他自己的混音器。 How can I apply the changes I'm doing in the main menu volume/s to be apply also in the volumes of the game scene audio mixer?如何将我在主菜单音量中所做的更改应用到游戏场景混音器的音量中?

This is a screenshot of the audio mixer in the Game scene, I added empty game object name it Game Audio:这是Game场景中音频混音器的截图,我添加了空游戏object,命名为Game Audio:

Game Audio settings游戏音频设置

The same settings copy of the settings for the main menu scene audio mixer:主菜单场景混音器设置的相同设置副本:

Screenshot of the main menu audio settings:主菜单音频设置截图:

main menu audio settings主菜单音频设置

Later I will change the music clip and sfx effects in the game scene.稍后我将更改游戏场景中的音乐剪辑和 sfx 效果。 The reason for two mixers each for main menu and game scenes is to be able to control the music and sfx individual in each scene.主菜单和游戏场景各有两个混音器的原因是能够控制每个场景中的音乐和音效。

This script Settings is in the Main Menu scene and control the music and sfx sliders volumes:此脚本设置位于主菜单场景中,并控制音乐和 sfx 滑块音量:

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
{
    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()
    {
        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("musicvolume");
        float sfxvolume = PlayerPrefs.GetFloat("sfxvolume");

        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("musicvol", Mathf.Log10(volume) * 20);
        musicText.text = Math.Round(volume * 100, MidpointRounding.AwayFromZero).ToString();

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

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

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

    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;
    }
} 

Now when I'm in the main menu scene the game scene is also still loaded and I want that when I change the volume/s in the main menu that it will effect applied to the game audio too.现在,当我在主菜单场景中时,游戏场景也仍在加载中,我希望当我在主菜单中更改音量时,它也会影响到游戏音频。 Not same music and sfx in the main menu like in the game but just to know that I changed the volume/s that it will effect the game scene too.主菜单中的音乐和音效与游戏中不同,但只是知道我更改了音量/秒,它也会影响游戏场景。

This script is in the Game scene.该脚本位于游戏场景中。 In the array objsToDisable I have 3 objects that I disable/enabled and in this array I want to be able to disable/enable the Game audio.在数组 objsToDisable 我有 3 个我禁用/启用的对象,在这个数组中我希望能够禁用/启用游戏音频。 I did Expose for both music and sfx in the game audio mixer but not sure how to SetFloat of the Game audio mixer?我在游戏混音器中为音乐和 sfx 做了 Expose,但不确定如何设置游戏混音器的 SetFloat? Where do I add the float or how do I use it in this script?我在哪里添加浮动或如何在此脚本中使用它?

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

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

    // Start is called before the first frame update
    void Start()
    {

    }

    // 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)
    {
        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);
            }
        }
    }
}

You can use DontDestroyOnLoad .您可以使用DontDestroyOnLoad If you call this in monobehaviour attached to audio mixer, it will stay on all scenes and hold the variable that given to it.如果您在附加到混音器的 monobehaviour 中调用它,它将保留在所有场景中并保存给它的变量。 Unless you call destroy manually.除非你手动调用destroy。 Now you have 2 audiomixer.现在你有 2 个混音器。 You can get user value from old audio mixer and set it for main scene audio mixer.您可以从旧的混音器中获取用户价值并将其设置为主场景混音器。 Then delete old one.然后删除旧的。 Also you should mute audio mixer that attached to DontDestroyOnLoad so only 1 audio source active at a time.此外,您应该静音连接到 DontDestroyOnLoad 的混音器,以便一次只有 1 个音频源处于活动状态。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从主菜单场景加载我的游戏后音频停止播放 - Audio stops playing after loading my game from the main menu scene 如何禁用特定场景的音频? - How can I disable the audio for a specific scene? 如何从主菜单设置场景将材质/颜色更改为 object - How can i change a material/color to an object from the main menu settings scene 我在主菜单做了一个调整游戏音量的滑块,但是当场景切换到实际游戏时音乐恢复正常? - Ive made a slider that adjust the games volume in the main menu but the music reverts to normal when the scene is changed to the actual game? 如何将多场景游戏转换为单场景游戏? - How can i convert a game with multiple scene into a single scene game? 如何检查主菜单后是否开始游戏? - How can I check if game is started after the main menu? 如何将我的分数游戏 Object 从我的游戏场景转移到 Unity 中的游戏结束场景 - How can I Transfer my Score Game Object from my game scene to game over scene in Unity How Can I save Ribbon Settings made in the Outlook Explorer Window and also display und use them in the Outlook MailItem Window? - How Can I save Ribbon Settings made in the Outlook Explorer Window and also display und use them in the Outlook MailItem Window? 我怎样才能在运行时画线,而不仅仅是在场景中作为小玩意儿? - How can I draw the lines also in runtime and not only in scene as gizmos? 如何创建随音频变化的显示 - How to Create a Display That Changes by Audio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM