在 Unity3d 中的多个场景之间切换时播放连续音乐
Play continuous music when swapping between multiple scene in Unity3d
我有以下场景
- 包含带有音频源组件的游戏对象的主菜单场景
- 一个关于我们的场景
游戏对象脚本:
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");
}
}
当我点击关于我们按钮时,Game Music object
继续播放,我可以听到音乐,但是当我 return 返回主菜单时,没有音乐仍在播放。当我 return 到主菜单并且音频监听器的音量值设置为 1
时,我可以看到游戏对象没有被破坏,但我无法弄清楚这个问题谁能帮忙我
为此你需要一个单例。
持久的单例
Sometimes you need your singletons to last between scenes (for
example, in this case you might want to play music during a scene
transition). One way to do this is to call DontDestroyOnLoad() on your
singleton.
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!
}
}
我有以下场景
- 包含带有音频源组件的游戏对象的主菜单场景
- 一个关于我们的场景
游戏对象脚本:
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");
}
}
当我点击关于我们按钮时,Game Music object
继续播放,我可以听到音乐,但是当我 return 返回主菜单时,没有音乐仍在播放。当我 return 到主菜单并且音频监听器的音量值设置为 1
时,我可以看到游戏对象没有被破坏,但我无法弄清楚这个问题谁能帮忙我
为此你需要一个单例。
持久的单例
Sometimes you need your singletons to last between scenes (for example, in this case you might want to play music during a scene transition). One way to do this is to call DontDestroyOnLoad() on your singleton.
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!
}
}