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

时间:2015-01-12 21:49:28

标签: c# unity3d singleton

我有以下情况

  1. 包含带有音频源组件的游戏对象的主菜单场景
  2. 一个关于我们的场景
  3. gameObject脚本:

    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();
        }
    }
    

    关于我们后退按钮脚本:

    using UnityEngine;
    using System.Collections;
    
    public class Back_btn : MonoBehaviour 
    {
         void OnMouseDown() 
         {
            Application.LoadLevel("MainMenu");
    
         }
     }
    

    enter image description here enter image description here

    当我点击aboutUs按钮时,Game Music object继续播放,我可以听到音乐,但当我返回主菜单时,仍然没有播放音乐。我可以看到,当我返回主菜单并且音频监听器将音量值设置为1时,游戏对象不会被破坏,但我无法解决问题,任何人都可以帮助我< / p>

1 个答案:

答案 0 :(得分:1)

你需要一个单身人士。 Unity Patternsa great post about singleton usage in Unity3D environment。尝试实现网站中指定的 Persistent Singleton 模式。

持久单身人士

  

有时你需要你的单身人士在场景之间持续(因为   例如,在这种情况下,您可能希望在场景中播放音乐   过渡)。一种方法是在你的上面调用DontDestroyOnLoad()   单。

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!
    }
}