使用 PlayerPrefs 保存数据
public void OnBackButtonDown()
{
PlayerPrefs.SetInt("gold", GameController.Instance.gold);//存储金币
PlayerPrefs.SetInt("lv", GameController.Instance.lv);//存储等级
PlayerPrefs.SetFloat("scd", GameController.Instance.smallTimer);//存储小计时器
PlayerPrefs.SetFloat("gold", GameController.Instance.bigTimer);//存储大奖励计时器
PlayerPrefs.SetInt("exp", GameController.Instance.exp);//存储经验
int temp = (AudioManager.Instance.IsMute == false) ? 0 : 1;
PlayerPrefs.SetInt("mute", temp);//存储静音开关
UnityEngine.SceneManagement.SceneManager.LoadScene(0);//跳转场景
}
读取数据
public class GameController : MonoBehaviour
{
private void Start()
{
//读取数据,如果没有则返回第二个参数
gold = PlayerPrefs.GetInt("gold", gold);
lv = PlayerPrefs.GetInt("lv", lv);
exp = PlayerPrefs.GetInt("exp", exp);
smallTimer = PlayerPrefs.GetFloat("scd", smallCountdown);
bigTimer = PlayerPrefs.GetFloat("bcd", bigCountdown);
}
}