C# 在Unity3d中的多个场景之间切换时播放连续音乐

C# 在Unity3d中的多个场景之间切换时播放连续音乐,c#,unity3d,singleton,C#,Unity3d,Singleton,我有下面的场景 包含带有音频源组件的游戏对象的主菜单场景 关于我们的一幕 游戏对象脚本: using UnityEngine; using System.Collections; public class ManageMusic : MonoBehaviour { private static ManageMusic _instance; public static ManageMusic instance { get {

我有下面的场景

  • 包含带有音频源组件的游戏对象的主菜单场景
  • 关于我们的一幕
  • 游戏对象脚本:

    using UnityEngine;
    using System.Collections;
    
    public class ManageMusic : MonoBehaviour
    {
        private static ManageMusic _instance;
    
        public static ManageMusic instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = GameObject.FindObjectOfType<ManageMusic>();
    
                    //Tell unity not to destroy this object when loading a new cene!
                    DontDestroyOnLoad(_instance.gameObject);
                }
    
                return _instance;
            }
        }
    
        private void Awake()
        {
            if (_instance == null)
            {
                Debug.Log("Null");
                //If I am the first instance, make me the Singleton
                _instance = this;
                DontDestroyOnLoad(this);
            }
            else
            {
    
                //If a Singleton already exists and you find
                //another reference in scene, destroy it!
                if (this != _instance)
                {
                    Play();
                    Debug.Log("IsnotNull");
                    Destroy(this.gameObject);
                }
            }
    
        }
        public void Update()
        {
            if (this != _instance)
            {
                _instance = null;
            }
        }
        public void Play()
        {
            this.gameObject.audio.Play();
        }
    }
    


    当我点击aboutUs按钮时,
    游戏音乐对象
    继续播放,我可以听到音乐,但当我返回主菜单时,没有音乐仍在播放。当我返回主菜单时,我可以看到游戏对象没有被破坏,音频监听器的音量值设置为
    1
    ,但我不知道问题出在哪里,有人能帮我吗你需要一个单件吗

    持久性单态 有时候,你需要你的单身在两个场景之间持续(例如 例如,在本例中,您可能希望在场景中播放音乐 过渡)。实现这一点的一种方法是在服务器上调用DontDestroyOnLoad() 辛格尔顿

    公共类MusicManager:MonoBehavior
    {
    私有静态MusicManager_实例;
    公共静态MusicManager实例
    {
    得到
    {
    if(_instance==null)
    {
    _instance=GameObject.FindObjectOfType();
    //告诉unity在加载新场景时不要破坏此对象!
    DontDestroyOnLoad(_instance.gameObject);
    }
    返回_实例;
    }
    }
    无效唤醒()
    {
    if(_instance==null)
    {
    //如果我是第一个,让我成为单身汉
    _实例=此;
    DontDestroyOnLoad(此);
    }
    其他的
    {
    //如果一个单例已经存在,并且您发现
    //场景中的另一个引用,销毁它!
    如果(此!=\u实例)
    摧毁(这个游戏对象);
    }
    }
    公共游戏
    {
    //播放一些音频!
    }
    }
    
    这个代码和我的不一样吗,如果我想测试你的代码,我应该怎么做?把我的换成你的还有另一个问题,我还没有试过你的代码,但在我的特殊情况下,如果在
    主菜单中创建了单例
    它的脚本将保留在
    关于
    场景中,但是当我返回主菜单时,单例是否会持续并继续播放,或者它会消失,或者它会再次播放?我不确定你的
    if else
    代码块是什么工作,但我敢打赌它不会像你想的那样工作。请继续阅读链接中的模式。使用此代码段并尝试对其进行更改,使其符合您的需要。此脚本将附加到的游戏对象将仅在游戏加载后创建,并且在更改场景时不会删除,因此音乐将一直播放,直到您停止播放为止(例如,当玩家将场景更改为游戏时,您告诉脚本仅在此时停止播放)。如果(\u instance==null),请将
    Play()
    移动到
    if,尝试禁用音乐上的“唤醒时播放”功能
    阻止并浏览场景?谢谢@Varaquilex我在制作预加载场景时设法修复了我的代码,我将音乐对象放入其中,并取消选中“唤醒时播放”,而在我的主菜单上,我将此脚本添加到我的
    开始
    函数中:if(!ManageMusic.instance.audio.isPlaying)ManageMusic.instance.play()@太好了!我很高兴我帮了忙。祝你的项目好运!
    using UnityEngine;
    using System.Collections;
    
    public class Back_btn : MonoBehaviour 
    {
         void OnMouseDown() 
         {
            Application.LoadLevel("MainMenu");
    
         }
     }
    
    public class MusicManager : MonoBehaviour 
    {
        private static MusicManager _instance;
     
        public static MusicManager instance
        {
            get
            {
                if(_instance == null)
                {
                    _instance = GameObject.FindObjectOfType<MusicManager>();
     
                    //Tell unity not to destroy this object when loading a new scene!
                    DontDestroyOnLoad(_instance.gameObject);
                }
     
                return _instance;
            }
        }
     
        void Awake() 
        {
            if(_instance == null)
            {
                //If I am the first instance, make me the Singleton
                _instance = this;
                DontDestroyOnLoad(this);
            }
            else
            {
                //If a Singleton already exists and you find
                //another reference in scene, destroy it!
                if(this != _instance)
                    Destroy(this.gameObject);
            }
        }
     
        public void Play()
        {
            //Play some audio!
        }
    }