1.静态变量传递
可以创建一个静态类,或者类里面有静态变量
2.创建一个不会被销毁的游戏物体
此方法有一个问题,即如果此物体所在的场景重复加载,则此物体重复存在。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PersistentData : MonoBehaviour {
public static PersistentData thePersistentData;
//要保存使用的数据;
[NonSerialized]
public int index = 0;
[NonSerialized]
public string ID = "identify";
//初始化
private void Awake()
{
if (thePersistentData == null)
{
DontDestroyOnLoad(gameObject);
thePersistentData = this;
}
}
// Update is called once per frame
void Update () {
}
}