Unity实现 场景切换 音乐不断

转自彪哥,我稍微略加改动下。。把我自己实现过程中容易犯错的点稍微点下。红字需注意

http://blog.csdn.net/xiebiao360/article/details/14093463

 

原文如下:

 

今天有人问我切换场景时背景音乐不能正常播放,于是写了个例子简单测试下,具体如下:

首先创建两个场景,为了区分两个场景(test1.unity & test2.unity)的不同,我们给第一个场景中加一个Cube(可以不建),并新建一个空物体GameObject,附加音频组件AudioSource,拖放音频文件上去,为保证这个空物体在场景切换时不被销毁,我们需要使用DontDestroyOnLoad(object)方法,新建一个C#脚本TestAudio

 

[csharp]  view plain copy
 
  1. using UnityEngine;  
  2. using System.Collections;    
  3. public class TestAudio : MonoBehaviour {    
  4.     void Start () {  
  5.     DontDestroyOnLoad(this.gameObject);  
  6.     }        
  7. }  

 

将此脚本拖放给GameObject,保持GameObject不被Destory掉

二、

再建一个C#脚本TestAudio2,用来绘制GUI进行切换场景

 

这里我直接贴出我的TestAudio2(我的起名叫audioXieBiao.cs)全部代码图如下

 

using UnityEngine;
using System.Collections;

public class audioXieBiao : MonoBehaviour {
     public GameObject objPrefabInstantSource;//文章中叫obje
     public GameObject musicInstant = null;//文章中叫obj
	// Use this for initialization
	void Start () {
        musicInstant = GameObject.FindGameObjectWithTag("sound");
        if (musicInstant==null)
        {
            musicInstant = (GameObject)Instantiate(objPrefabInstantSource);
        }
	}
    void OnGUI()
    {
        if (GUILayout.Button("Load Level"))
        {
            if (Application.loadedLevelName == "test1")//关于这个下面有详细介绍
            {
                Application.LoadLevel("test2");
            }
            else
            {
                Application.LoadLevel("test1");
            }
        }
        if (GUI.Button(new Rect(10, 90, 120, 30), "Pause"))
        {
            musicInstant.audio.Pause();
        }
        if (GUI.Button(new Rect(10, 130, 120, 30), "Stop"))
        {
            musicInstant.audio.Stop();
        }
        if (GUI.Button(new Rect(10, 50, 120, 30), "Play"))
        {
            musicInstant.audio.Play();
        }
    }
}

 

 

 

 

 

[csharp]  view plain copy
 
  1. if (GUI.Button(new Rect(10,10,120,30),"load level")) {  
  2.     if (Application.loadedLevelName=="test1") {  
  3.         Application.LoadLevel("test2");  
  4.     }  
  5.     else {  
  6.         Application.LoadLevel("test1");  
  7.     }  
  8. }  

在这里我们拖放给相机就可以了
另外要注意一个地方,当我们从第一个场景test1切换到test2的时候没有问题,但是从test2切换回test1的时候,场景里会多出一个GameObject,此时会出现两个声音(一个是test1里重新生成的,另一个是我们之前没有销毁的),为了解决这个问题我们把GameObject设为预设,并加个标签sound

编辑TestAudio2中的代码

 

[csharp]  view plain copy
 
  1. public GameObject obje;  
  2. GameObject obj=null;  
  3. // Use this for initialization  
  4. void Start () {  
  5.     obj = GameObject.FindGameObjectWithTag("sound");  
  6.     if (obj==null) {  
  7.         obj = (GameObject)Instantiate(obje);  
  8.     }     
  9. }  

拖放Project中的预设GameObject到obje(此时hierarchy中只有cube和camera)

 

 

运行,切换场景时,背景音乐不间断播放

接下来可以对背景音乐进行控制,如播放,暂停,停止

 

[csharp]  view plain copy
 
  1. if (GUI.Button(new Rect(10,90,120,30),"Pause")) {  
  2.     obj.audio.Pause();  
  3. }  
  4. if (GUI.Button(new Rect(10,130,120,30),"Stop")) {  
  5.     obj.audio.Stop();  
  6. }  
  7. if (GUI.Button(new Rect(10,50,120,30),"Play")) {  
  8.     obj.audio.Play();  
  9. }  

 

 

 

附加学习:如何获得场景名称 

检验结果为下:Unity实现 场景切换 音乐不断_第1张图片

 

输出为:

Unity实现 场景切换 音乐不断_第2张图片

 

 

 

  //如何当前场景名称 方法一
        Debug.Log("方法一:" + Application.loadedLevelName);//当所在为test1.unity时,输出test1,当所在为test2.unity时,输出test2.
        //方法二
        string currentSceneName = UnityEditor.EditorApplication.currentScene;//这样输出的是Assert目录下的完整目录:Assets/Scenes/test1.unity
        Debug.Log("currentSceneName输出为:"+currentSceneName);
        string[] nameS= currentSceneName.Split('/');
        Debug.Log("方法一:" + nameS[nameS.Length - 1]);//始终显示test1.unity()

 

 

 

 

 

你可能感兴趣的:(总结笔记,unity音频)