今天学习了如何存读档。
采用了Unity自带的方式PlayerPrefs
写了一个示例代码
功能是:建立一个名字的新档,每次打开游戏名字都会变。
PlayerPrefs.SetString("save","kkk");//创建名为save数据,存一个字符串kkk
PlayerPrefs.GetString("save");;//访问
PlayerPrefs.HasKey("save")//判断有没有存档
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class SaveLoad : MonoBehaviour
{
// Start is called before the first frame update
public TMP_Text _save;
void Start()
{
if(PlayerPrefs.HasKey("save")){
string names = PlayerPrefs.GetString("save");
Debug.Log(names);
_save.text = names;
if(names == "Jack"){
PlayerPrefs.SetString("save","kkk");
}
else{
PlayerPrefs.SetString("save","Jack");
}
}
else{
PlayerPrefs.SetString("save","Jack");
Debug.Log("OK");
}
}
// Update is called once per frame
void Update()
{
}
}