Unity场景跳转时音乐继续播放

1.首先将音频文件挂载在MainCamera上
在这里插入图片描述
2.编写脚本控制跳转场景时音乐继续播放,不自动销毁

public class MusicController : MonoBehaviour {

    static MusicController instance = null;
    public static MusicController Instance
    {
        get
        {
            return instance;
        }
    }

    void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(this.gameObject);
            return;
        }
        else
        {
            instance = this;
        }
        DontDestroyOnLoad(this.gameObject);//使对象目标在加载新场景时不被自动销毁
    }
	
}

3.将脚本挂载在MainCamera上,效果如下
在这里插入图片描述
点击开始游戏跳转场景后,音乐继续播放
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_37176118/article/details/85632532