c# - Unity - 在游戏中添加背景音乐

标签 c# unity3d

我想在我的游戏中添加一些可以在每个场景中播放的音乐,如果场景改变音乐不会重新开始并且可以在设置菜单中关闭

有人可以帮我弄清楚吗?

最佳答案

到目前为止,您尝试过什么?显示您的代码。有一种方法可以实现你想要的,那就是使用 DontDestroyOnLoad 函数。创建一个游戏对象,将 AudioSource 添加到其中,然后将以下脚本添加到该游戏对象:

public class AudioPlayerManager: MonoBehaviour
{
      private static AudioPlayerManager instance = null;
      private AudioSource audio;

      private void Awake()
      {
          if (instance == null)
          { 
               instance = this;
               DontDestroyOnLoad(gameObject);
               return;
          }
          if (instance == this) return; 
          Destroy(gameObject);
      }

      void Start()
      {
         audio = GetComponent<AudioSource>();
         audio.Play();
      }
}

关于c# - Unity - 在游戏中添加背景音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59086739/

相关文章:

c# - 在 ASP.NET Web 应用程序中禁用 Razors 默认 .cshtml 处理程序

c# - 为什么这个 c# 片段是合法的?

c# - 将方向应用到当前变换位置

image - 在 Unity 3D 中更改按钮图像

audio - Unity只播放少数游戏对象的声音

c# - Unity HTC VIVE 远距传送

c# - Unity - 麦克风检查是否静音

c# - 如何在网格中添加多个自定义形状的按钮?

c# - StructureMap:如何在与 ConnectImplementationsToTypesClosing 连接的类型上设置生命周期

c# - 在哪里可以找到最新的 Google Docs API 以及示例的源代码?