【问题标题】:How can I disable the audio for a specific scene?如何禁用特定场景的音频?
【发布时间】:2021-01-13 23:41:07
【问题描述】:

我正在使用这条线:

AudioListener.pause = true;

问题是我加载了两个场景,我正在加载另一个场景

LoadSceneMode.Additive

然后音频暂停会影响两个场景。

这是我暂停/恢复游戏的完整代码。索引 0 是主菜单场景,索引 1 是游戏场景。我有两个场景,主菜单和游戏。

当我按下主菜单的退出键时,我想暂停游戏场景中的音频,然后当再次按下退出键回到游戏时,我想恢复游戏场景中的音频。

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);
                AudioListener.pause = false;
                SceneManager.UnloadSceneAsync(0);
                Cursor.visible = false;
                Time.timeScale = 1;
            }
            else
            {
                Time.timeScale = 0;
                MenuController.LoadSceneForSavedGame = false;
                AudioListener.pause = true;
                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);
            }
        }
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    如前所述,一种可能性是使用AudioSource.mute,但当然你必须在相应场景的所有来源上使用它。

    当您已经知道索引时,您可以这样做,例如

    var scene = SceneManager.GetSceneAtBuildIndex(1);
    if(scene.isValid)
    {
        // Iterate through the scene Hierarchy and get all sources (also inactive/disabled ones)
        var audioSources = new List<AudioSource>();
        foreach (var root in scene.GetRootGameObjects())
        {
            audioSources.AddRange(root.GetComponentsInChildren<AudioSource>(true));
        }
    
        foreach(var source in audioSources)
        {
            source.mute = !enabled;
            //Or
            source.pause = !enabled;
        }
    }
    

    您也可以使用AudioMixer,我认为这更方便。

    这里将简单但所有的来源变成某个AudioMixerGroup,并使音量成为一个公开的参数。

    然后您可以简单地使用 SetFloat 使整个组静音。


    顺便说一句,在命名你的方法参数时要非常小心,比如已经存在的字段/属性!是的,无论如何它们都会被掩盖,但这只会给您和其他读者带来困惑。 MonoBehavior 已经存在 Behavior.enabled,因此您的类派生自。

    【讨论】:

      【解决方案2】:

      您可以尝试使用audio_source_name.mute = true 和/或audio_source_name.Stop() 吗?

      例子:

      void Update()
      {
          if (Input.GetKeyDown(KeyCode.Escape))
          {
              if (Time.timeScale == 0)
              {
                  DisableEnableUiTexts(true);
                  // AudioListener.pause = true;
                  audio_source_name.mute = false;     // <--------
                  SceneManager.UnloadSceneAsync(0);
                  Cursor.visible = false;
                  Time.timeScale = 1;
              }
              else
              {
                  Time.timeScale = 0;
                  MenuController.LoadSceneForSavedGame = false;
                  // AudioListener.pause = true;
                  audio_source_name.mute = false;     // <--------
                  SceneManager.LoadScene(0, LoadSceneMode.Additive);
                  SceneManager.sceneLoaded += SceneManager_sceneLoaded;
      
                  Cursor.visible = true;
              }
          }
      }
      

      你也可以这样做(来源:Unity - Scripting API: AudioSource.mute):

      // Mutes-Unmutes the sound from this object each time the user presses space.
      using UnityEngine;
      using System.Collections;
      
      public class ExampleClass : MonoBehaviour
      {
          AudioSource audioSource;
      
          void Start()
          {
              audioSource = GetComponent<AudioSource>();
          }
      
          void Update()
          {
              if (Input.GetKeyDown(KeyCode.Space))
                  audioSource.mute = !audioSource.mute;
          }
      }
      

      【讨论】:

      • 这个禁用 one 某些 AudioSource .. OP 的问题是:你能禁用来自某个场景的所有声音
      猜你喜欢
      • 1970-01-01
      • 2019-03-01
      • 2012-04-06
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多