using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerPrefsDemo : MonoBehaviour
{
private void Update()
{
if(Input.GetKeyDown(KeyCode.S))
{
PlayerPrefs.SetInt("学号",9527);
PlayerPrefs.SetString("姓名","莫小白");
PlayerPrefs.SetFloat("分数",100.0f);
//用于在突发退出程序时,保存数据以备恢复时使用,但是会导致程序间断所以不建议调用。
PlayerPrefs.Save();
print("信息已存储,可以浪了!");
}
//将存储的数据打印出来
if (Input.GetKeyDown(KeyCode.P))
{
int id = PlayerPrefs.GetInt("学号");
string name = PlayerPrefs.GetString("姓名");
float score = PlayerPrefs.GetFloat("分数");
if(!string.IsNullOrEmpty(name) && score >= 0)
{
Debug.Log(id+"\t"+name+"\t"+score);
}
else
{
Debug.Log("无数据可查!");
}
}
//将数据删除
if(Input.GetKeyDown(KeyCode.D))
{
if(PlayerPrefs.HasKey("学号"))
{
PlayerPrefs.DeleteKey("学号");
PlayerPrefs.DeleteAll();
Debug.Log("已完成对数据的删除!");
}
else
{
Debug.Log("无数据可删!");
}
}
}
}