当前位置:网站首页>Unity-存档与读档
Unity-存档与读档
2022-08-02 14:11:00 【莉萝爱萝莉】
1. PlayerPrefs。
采用键值对的方式对数据进行存储。
PlayerPrefs.SetInt("Index",1);
// 可以存储Int, Float, String类型的数据。
PlayerPrefs.SetFloat("Height",183.5f);
PlayerPrefs.SetString("Name","Tom");
// 查看数据是否存在
if (PlayerPrefs.HasKey("Index"))
// 获取数据:
PlayerPrefs.GetInt("Index");
2. 二进制保存
- 首先创建可实例化的Save类
[System.Serializable]
public class Save
{
public string stringData;
public float floatData;
public int intData;
// 三维坐标
float[] pos = new float[3];
// 设置坐标
public void PosSet(Vector3 vector3)
{
pos[0] = vector3.x;
pos[1] = vector3.y;
pos[2] = vector3.z;
}
// 读取坐标
public Vector3 PosGet()
{
return new Vector3(pos[0], pos[1], pos[2]);
}
}
因为三维向量坐标不能直接保存,所以需要间接保存
引入using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
2. 保存
public void Save()
{
// 二进制保存
Save save = new Save();
save.stringData = stringObj.GetComponent<DataText>().inputField.text;
save.floatData = float.Parse(floatObj.GetComponent<DataText>().inputField.text);
save.intData = int.Parse(intObj.GetComponent<DataText>().inputField.text);
save.PosSet(dragObj.transform.position);
// 创建一个二进制格式化程序
BinaryFormatter bf = new BinaryFormatter();
// 创建一个文件流
// 位置:C:\Users\用户名\AppData\LocalLow\公司名\项目名
FileStream file = File.Create(Application.persistentDataPath + "/gamesave.save");
bf.Serialize(file, save);
file.Close();
Debug.Log("已存档");
}
- 读取
public void Load()
{
// 如果文件存在
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();
stringObj.GetComponent<DataText>().inputField.text = save.stringData;
floatObj.GetComponent<DataText>().inputField.text = save.floatData.ToString();
intObj.GetComponent<DataText>().inputField.text = save.intData.ToString();
dragObj.transform.position = save.PosGet();
Debug.Log("已读档");
}
}
3. XML保存
- 实体类
using System.Collections.Generic;
using System.Xml.Serialization;
[System.Serializable]
public class XmlSerlize
{
// 属性
[XmlAttribute("Id")]
public int Id {
get; set; }
[XmlAttribute("Name")]
public string Name {
get; set; }
// 元素内容
[XmlElement("List")]
public List<int> Infors {
get; set; }
}
- 保存
void SerilizeSave()
{
// 初始化
XmlSerlize xmlSerlize = new XmlSerlize();
xmlSerlize.Id = 1;
xmlSerlize.Name = "liluo";
xmlSerlize.Infors = new List<int>() {
1, 2, 3, 4 };
XmlSerializer xml = new XmlSerializer(xmlSerlize.GetType());
// 创建一个文件流
FileStream file = new FileStream(Application.persistentDataPath + "/save.xml", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(file, System.Text.Encoding.UTF8);
xml.Serialize(sw, xmlSerlize);
sw.Close();
file.Close();
}
- 读取
void SerilizeLoad()
{
FileStream file = new FileStream(Application.persistentDataPath + "/save.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
XmlSerializer xml = new XmlSerializer(typeof(XmlSerilize));
XmlSerilize xmlSerlize = (XmlSerilize)xml.Deserialize(file);
file.Close();
Debug.Log(xmlSerlize.Id);
Debug.Log(xmlSerlize.Name);
foreach (var xmls in xmlSerlize.Infors)
Debug.Log(xmls);
}
边栏推荐
- Spark及相关生态组件安装配置——快速回忆
- General syntax and usage instructions of SQL (picture and text)
- Mysql的锁
- Use tencent cloud builds a personal blog
- 第三十一章:二叉树的概念与性质
- Introduction to in-order traversal (non-recursive, recursive) after binary tree traversal
- General code for pytorch model to libtorch and onnx format
- Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
- Configure clangd for vscode
- 2.登录退出,登录状态检查,验证码
猜你喜欢
随机推荐
KiCad常用快捷键
背包问题-动态规划-理论篇
3. User upload avatar
General syntax and usage instructions of SQL (picture and text)
GMP scheduling model of golang
How to solve Win11 without local users and groups
第三十三章:图的基本概念与性质
Summarize computer network super comprehensive test questions
Lightweight AlphaPose
How to simulate 1/3 probability with coins, and arbitrary probability?
Network Security Packet Capture
轻量化AlphaPose
lua编程
pygame image rotate continuously
2342. 数位和相等数对的最大和 哈希优化
总结计算机网络超全面试题
How to update Win11 sound card driver?Win11 sound card driver update method
Win10 Settings screen out from lack of sleep?Win10 set the method that never sleep
[STM32 Learning 1] Basic knowledge and concepts are clear
MATLAB绘图函数plot详解