我想在我的游戏中添加一些可以在每个场景中播放的音乐,如果场景改变音乐不会重新开始并且可以在设置菜单中关闭
有人可以帮我弄清楚吗?
最佳答案
到目前为止,您尝试过什么?显示您的代码。有一种方法可以实现你想要的,那就是使用 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/