Unity跳转场景传递数据
学习记录
跳转场景时,保证物体不被摧毁,可以保存场景数据内容
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class DontDestory : SingletonM<DontDestory>
{
/// <summary>
/// 单例
/// </summary>
static DontDestory instance;
protected override void Init()
{
base.Init();
// 这样就可以保证这个物体不被摧毁,那么它的子物体也就不会被摧毁,所以可以用来要保存每个场景数据的内容,分别创建
//在该父物体下。
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this);
}
// 如果这个静态变量已经有了, 那么就要删除掉,保留整个游戏只有一个
else if (instance != null)
{
Debug.Log(SceneManager.GetActiveScene().name + "被摧毁的物体名字" + gameObject.name);
Destroy(gameObject);
}
}
public void Update()
{
}
}