当前位置:网站首页>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);
}
边栏推荐
- 第二十九章:树的基本概念和性质
- Manifest merger failed : Attribute [email protected] value=
- 十天学习Unity3D脚本(一)九个回调
- LeetCode 2344. 使数组可以被整除的最少删除次数 最大公约数
- Network Security Packet Capture
- couldn't find 'libflutter.so' --flutter
- Codeforces Round #624 (Div. 3)
- jest test, component test
- Article pygame drag the implementation of the method
- Mysql lock
猜你喜欢

What should I do if I install a solid-state drive in Win10 and still have obvious lags?

MATLAB绘图命令fimplicit绘制隐函数图形入门详解

In-depth understanding of Golang's Map

十天学习Unity3D脚本(一)九个回调

Redis常见面试题

二叉树创建之层次法入门详解

5.事务管理

Detailed introduction to drawing complex surfaces using the plot_surface command

Yolov5 official code reading - prior to transmission

基于矩阵计算的线性回归分析方程中系数的估计
随机推荐
第二十七章:时间复杂度与优化
mysql的索引结构为什么选用B+树?
Mysql之MVCC
IPV4和IPV6是什么?
What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
LeetCode 2343. 裁剪数字后查询第 K 小的数字 暴力+语法考察
Please make sure you have the correct access rights and the repository exists. Problem solved
动态数组-vector
cmake configure libtorch error Failed to compute shorthash for libnvrtc.so
MATLAB绘图函数ezplot入门详解
计算机导论——数据库
MATLAB图形加标注的基本方法入门简介
倍增和稀疏表
golang之GMP调度模型
Redis common interview questions
C语言函数参数传递模式入门详解
第三十一章:二叉树的概念与性质
推开机电的大门《电路》(三):说说不一样的电阻与电导
求解斐波那契数列的若干方法
第二十五章:一文掌握while循环