转载:https://blog.csdn.net/kmyhy/article/details/78643293
概念
在 Unity 中关于保存,有 4 个关键的概念:
PlayerPrefs: 这是一个特殊的缓存系统,在两次游戏过程中为玩家保存简单设定。许多新手程序员误以为他们能够用它来保存游戏存档,但这不是什么好的做法。它只允许你保存简单的东西,比如图形声音设置、登录信息或者其它基本的用户相关的数据。
序列化:这是 Unity 真正用到的关键点。序列化将对象转换成字节流。听起来有点抽象,你可以看看这张图:
什么是“对象”?在这里“对象”是 Unity 中的任意脚本或文件。事实上,当你创建一个 MonoBehaviour 脚本时,Unity 会通过序列化/反序列化将文件转换成 C++ 代码,然后变成你在检查器窗口中看见的 C# 代码。如果你曾经添加过 [SerializeField] 让某些东西在检查器中显示出来,你就知道怎么回事了。
注意:如果你是一个 Java 或者 web 开发者,你可能熟悉 marshalling (编码解码)的概念。序列化和 marshalling 是几乎是同义词,如果你硬要说它们有什么不同,序列化是将对象从一种对象转换成另一种形式(比如,对象->字节),而 marshalling 是让参数从一个地方传到另一个地方。
反序列化:就是它的名字所暗示的。它是序列化反面,将一个字节流转换成对象。
JSON:全称是 JavaScript Object Notation,是一种语言无关的发送和接收数据的常用格式。例如,你可能有一个运行在 Java 或者 PHP 下面的 web 服务器。你不能发送 C# 对象给它,但你可以发送这个对象的 JSON 形式给它,服务器会用它来重新创建一个本地版本的对象。后面的内容中你会学习更多关于这方面的内容,现在你只需要理解它是一种简单的格式化数据让它能够跨平台的方法(就像 XML)。当进行“对象->JSON”和“JSON->对象”转换时,可以分别将这两种称作称作 JSON 序列化和JSON 反序列化。
Player Prefs
Save.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[Serializable]
public class Save
{
public int score = 0;
}
pPlayMusicSetting.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayMusicSetting : MonoBehaviour
{
[SerializeField]
private Toggle toggle;
[SerializeField]
private AudioSource audioSource;
private void Awake()
{
if(!PlayerPrefs.HasKey("music"))
{
PlayerPrefs.SetInt("music", 1);
toggle.isOn = true;
audioSource.enabled = true;
PlayerPrefs.Save();
}
else
{
if(PlayerPrefs.GetInt("music")==1)
{
toggle.isOn = true;
audioSource.enabled = true;
}
else
{
toggle.isOn = false;
audioSource.enabled = false;
}
}
}
public void settingMusic()
{
if(toggle.isOn)
{
PlayerPrefs.SetInt("music", 1);
audioSource.enabled = true;
}
else
{
PlayerPrefs.SetInt("music", 0);
audioSource.enabled = false;
}
PlayerPrefs.Save();
}
}
利用BinaryFormatter保存数据和json格式保存数据
game.cs
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using UnityEngine.UI;
public class game : MonoBehaviour
{
public int scores = 0;
public Text scoreText;
public Button loadButton;
public Button saveButton;
public Button gainButton;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void GainGameobject()
{
scores++;
scoreText.text = "live:" + scores;
}
//初始化save类中所有的数据
private Save CreateSaveGameObject()
{
Save save = new Save();
save.score = scores;
return save;
}
public void SaveGame()
{
Save save = CreateSaveGameObject();
//使用BinaryFormatter二进制序列化存数据
BinaryFormatter bf = new BinaryFormatter();
//persistentDataPath包含持久数据目录的路径(只读)
FileStream file = File.Create(Application.persistentDataPath + "/gamesave.save");
//将对象序列化给对应的流
bf.Serialize(file, save);
//关闭文件
file.Close();
scores = 0;
scoreText.text = "live:" + scores;
print("game save");
}
public void LoadGame()
{
if(File.Exists(Application.persistentDataPath+"/gamesave.save"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/gamesave.save", FileMode.Open);
Save save = (Save)bf.Deserialize(file);
file.Close();
scoreText.text = "score:" + save.score;
scores = save.score;
print("game save");
SaveAsJSON();
}
else
{
print("no game save");
}
}
//保存为json格式
public void SaveAsJSON()
{
Save save = CreateSaveGameObject();
//创建json字符串
string json = JsonUtility.ToJson(save);
// JSON 转换成一个 Save 对象
//Save save = JsonUtility.FromJson<Save>(json);
print("Saving as JSON:" + json);
}
}