当前位置:   article > 正文

unity切换场景,物体不销毁_不能销毁当前场景

不能销毁当前场景

在跳转场景的触发事件中,将背景音乐绑定的对象做成预制体,上面绑定了播放音乐的脚本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();
    }
   
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

这样就可以让场景0的物体出现在场景1里了,如下:
在这里插入图片描述
如果你不想要这个物体,只想要对应的脚本,那你可以把那个脚本写成单例就可以访问了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/332806
推荐阅读
相关标签
  

闽ICP备14008679号