简体   繁体   English

从主菜单场景加载我的游戏后音频停止播放

[英]Audio stops playing after loading my game from the main menu scene

When I pause the game, the audio pauses too, but I created a "go back to menu" button, it goes to the main menu but when I hit start again and the game starts running, my audio is dead, I can not hear anything.当我暂停游戏时,音频也会暂停,但我创建了一个“返回菜单”按钮,它会进入主菜单,但是当我再次点击开始并开始运行游戏时,我的音频已死,我听不到任何事物。

Here is my code:这是我的代码:

using UnityEngine; using UnityEngine.SceneManagement;

public class pausemenu : MonoBehaviour { public GameObject pauseMenu;

 public bool isPaused;
 
 void Start()
 {
     pauseMenu.SetActive(false);
 }
 // Update is called once per frame
 void Update()
 {
     if(Input.GetKeyDown(KeyCode.Escape))
     {
         if(isPaused)
         {
             ResumeGame();
         }
         else
         {
             PauseGame();
         }
     }
      
 }   
 
 
 public void PauseGame()
 {
     pauseMenu.SetActive(true);
     Time.timeScale = 0f;
     isPaused = true;
     AudioListener.pause = true;
 }
 
 public void ResumeGame()
 {
     pauseMenu.SetActive(false);
     Time.timeScale = 1f;
     isPaused = false;
     AudioListener.pause = false;
 }
  
 public void GoToMainMenu()
 {
     Time.timeScale = 1f;
     SceneManager.LoadScene("menu1");
 } 
 
 public void QuitTheGame()
 {
     Application.Quit();
 }
   
}

In the PauseGame() function you are setting the AudioListener.pause to true.在 PauseGame() 函数中,您将 AudioListener.pause 设置为 true。 So, in the GoToMainMenu() function set the AudioListener.pause to false.因此,在 GoToMainMenu() 函数中将 AudioListener.pause 设置为 false。

If you pause your game and call PauseGame() AudioListener.pause is set to true .如果您暂停游戏并调用PauseGame() AudioListener.pause设置为true

If you resume you correctly set AudioListener.pause to false .如果您恢复,则正确地将AudioListener.pause设置为false

However, if you stop your game (by going into some main menu) and start a new one (or the previous one) the static bool AudioListener.pause is still false because of your pause menu intervention.然而,如果你停止你的游戏(通过进入一些主菜单)并开始一个新的(或前一​​个)静态 bool AudioListener.pause由于你的暂停菜单干预仍然是false的。

Even if your starting setup results in pausemenu.Start() getting called when starting a new game, this will not affect AudioListener.pause .即使您的开始设置导致在开始新游戏时调用pausemenu.Start() ,这也不会影响AudioListener.pause You have to explicitly call pausemenu.ResumeGame() .您必须明确调用pausemenu.ResumeGame()

暂无
暂无

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

相关问题 如何控制在主菜单中所做的音频更改也应用于游戏场景音频设置? - How can I control the changes of the audio made in main menu to be applied also to the in game scene audio settings? 直接播放场景时,场景保存和加载工作正常,但是当我从主菜单加载时,它是 null 异常 - Scene save&load works fine when playing scene directly,but when i load from main menu it's null exception MediaElement随机停止播放MemoryStream的音频 - MediaElement randomly stops playing audio from MemoryStream 音频不会在特定场景中停止播放 - Audio not stop playing in a specific scene 从不同的场景加载特定的 UI 菜单 - Loading Particular UI Menu from Different Scene 如何将我的分数游戏 Object 从我的游戏场景转移到 Unity 中的游戏结束场景 - How can I Transfer my Score Game Object from my game scene to game over scene in Unity 我在主菜单做了一个调整游戏音量的滑块,但是当场景切换到实际游戏时音乐恢复正常? - 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? 主脑游戏的主菜单 - Main Menu for a mastermind game 如何检查主菜单后是否开始游戏? - How can I check if game is started after the main menu? 单击按钮以序列化数据和更改场景时,游戏停止,但是Unity没有错误日志 - Game stops when button is clicked to serialize data and change scene, but no error log from Unity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM