在跳转场景的触发事件中,将背景音乐绑定的对象做成预制体,上面绑定了播放音乐的脚本test1和audioSource组件
test1.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class test1 : MonoBehaviour { //public static test1 instance; private
AudioSource source; private void Awake() { //instance = this; source = transform
.GetComponent<AudioSource>(); source.volume = 0.5f; source.Play(); } }
play脚本随便挂载到一个物体身上,通过按钮触发click方法
using System.Collections; using System.Collections.Generic; using UnityEngine;
using UnityEngine.SceneManagement; public class play : MonoBehaviour { public
GameObject cube;//预制体 public static bool isHave = false; private GameObject
clone; public void click() { if(!isHave) { clone = GameObject.Instantiate(cube);
clone.transform.position = Vector3.zero; isHave = true; } DontDestroyOnLoad(
clone); SceneManager.LoadScene(1); } }
这样就可以让场景0的物体出现在场景1里了,如下:
如果你不想要这个物体,只想要对应的脚本,那你可以把那个脚本写成单例就可以访问了