1.两个场景
(1)第一个场景新建空物体GameObject挂上AudioSource组件,脚本挂上,把Tag改成sounds,再做成预设体。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
void Start ()
{
DontDestroyOnLoad(this.gameObject);
}
}
(2)在全部场景的摄像机上,挂另一个脚本。其他场景的预设体setactive=false。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Test : MonoBehaviour
{
public GameObject objPrefabInstantSource;//音乐预设体
private GameObject musicInstant = null;//判断场景中是否有这个物体
void Start ()
{
musicInstant = GameObject.FindGameObjectWithTag("sounds");
if (musicInstant==null)
{
musicInstant = (GameObject)Instantiate(objPrefabInstantSource);
}
}
public void OnClick()
{
SceneManager.LoadScene(1);
}
}