Unity3D 本地数据持久化几种方式
2024-09-27 04:08:42
下面介绍几种 Unity本地记录存储的实现方式。
第一种 Unity自身提供的 PlayerPrefs
//保存数据
-
PlayerPrefs.SetString("Name",mName);
-
PlayerPrefs.SetInt("Age",mAge);
-
PlayerPrefs.SetFloat("Grade",mGrade)
//读取数据
-
mName=PlayerPrefs.GetString("Name","DefaultValue");
-
mAge=PlayerPrefs.GetInt("Age",0);
-
mGrade=PlayerPrefs.GetFloat("Grade",0F);
//清除所有记录
PlayerPrefs.DeleteAll();
//删除其中某一条记录
PlayerPrefs.DeleteKey("Age");
//将记录写入磁盘
PlayerPrefs.Save()
第二种 BinaryFormatter 二进制序列化
假设有一个Player类
-
[System. Serializable]
-
public class Player
-
{
-
public int health;
-
public int power;
-
public Vector3 position;
-
}
由于BinaryFormatter序列化不支持Unity的Vector3类型,所以我们需要做一下包装。
-
public class PlayerData{
-
-
public int level;
-
public int health;
-
public float[] position;
-
-
public PlayerData(Player player)
-
{
-
this.level = player.level;
-
this.health = player.health;
-
this.position = new float[3];
-
this.position[0] = player.transform.position.x;
-
this.position[1] = player.transform.position.y;
-
this.position[2] = player.transform.position.z;
-
}
-
}
我们对PlayerData进行保存和读取。读取出来的PlayerData可以赋给Player。
-
public static class SaveSystem{
-
//保存数据
-
public static void SavePlayer(Player player)
-
{
-
BinaryFormatter formatter = new BinaryFormatter();
-
string path = Application.persistentDataPath+"/player.fun";
-
FileStream stream = new FileStream(path,FileMode.Create);
-
PlayerData data = new PlayerData(player);
-
formatter.Serialize(stream,data);
-
stream.Close();
-
}
-
-
//读取数据
-
public static PlayerData LoadPlayer()
-
{
-
string path = Application.persistentDataPath+"/player.fun";
-
if(File.Exists(path))
-
{
-
BinaryFormatter formatter = new BinaryFormatter();
-
FileStream stream = new FileStream(path,FileMode.Open);
-
PlayerData data = formatter.Deserialize(stream) as PlayerData;
-
stream.Close();
-
return data;
-
}else{
-
Debug.LogError("Save file not found in "+path);
-
return null;
-
}
-
}
-
}
第三种 保存为json格式的文本文件
使用 Unity 自身API JsonUtility。
保存数据
-
public static void SavePlayerJson(Player player)
-
{
-
string path = Application.persistentDataPath+"/player.json";
-
var content = JsonUtility.ToJson(player,true);
-
File.WriteAllText(path,content);
-
}
读取数据
-
public static PlayerData LoadPlayerJson()
-
{
-
string path = Application.persistentDataPath+"/player.json";
-
if(File.Exists(path)){
-
var content = File.ReadAllText(path);
-
var playerData = JsonUtility.FromJson<PlayerData>(content);
-
return playerData;
-
}else{
-
Debug.LogError("Save file not found in "+path);
-
return null;
-
}
-
}
第四种 XmlSerializer进行串行化
假如有类
-
public class Entity
-
{
-
public Entity()
-
{
-
}
-
public Entity(string c, string f)
-
{
-
name = c;
-
school = f;
-
}
-
public string name;
-
public string school;
-
}
读取数据
-
List<Entity> entityList=null;
-
XmlSerializer xs = new XmlSerializer(typeof(List<Entity>));
-
using (StreamReader sr = new StreamReader(configPath))
-
{
-
entityList = xs.Deserialize(sr) as List<Entity>;
-
}
保存数据
-
List<Entity> entityList=null;
-
XmlSerializer xs = new XmlSerializer(typeof(List<Entity>));
-
using (StreamWriter sw = File.CreateText(configPath))
-
{
-
xs.Serialize(sw, entityList);
-
}
对应的xml文件为:
-
<?xml version="1.0" encoding="utf-8"?>
-
<ArrayOfEntity xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-
<Entity>
-
<Name>Alice</Name>
-
<School>SJTU</School>
-
</Entity>
-
<Entity>
-
<Name>Cici</Name>
-
<School>CSU</School>
-
</Entity>
-
<Entity>
-
<Name>Zero</Name>
-
<School>HIT</School>
-
</Entity>
-
</ArrayOfEntity>
最新文章
- 关于javascript的运动教程
- sh 测试网段在线主机
- 使用ycsb测试cassandra
- mysql where执行顺序
- ok6410 android driver(12)
- 通过表达式、函数给React组件属性赋值
- Condition-线程通信更高效的方式
- 完整的站内搜索Demo(Lucene.Net+盘古分词)
- 第2个Wiindows程序讲解
- 到处是坑的微信公众号支付开发(java)
- 遍历map的几种方式
- [JLOI2015]装备购买
- OpenStack-Keystone(2)
- JS的javascript:void(0)用法
- anaconda安装Opencv报错:Could NOT find PythonLibs: Found unsuitable version ";2.7.6";,
- SpringMVC学习(三)———— springmvc的数据校验的实现
- vee-validate表单验证组件
- 饿了么element UI<;el-dialog>;弹出层<;/el-dialog>;修改默认样式不能在<;style scoped>;修改
- VS2015+MySql+EF6采坑经验总结
- FileInputStram入门
热门文章
- [c++] 分号的使用
- 笔记本用HDMI转VGA 使用双屏办公 听语音
- MySQL报错ERROR 1436 (HY000): Thread stack overrun:
- Ubuntu 软件更新 系统升级
- Linux服务之DHCP服务篇(scp)
- dd命令详解-(转自dkcndk)
- 04丨MongoDB特色及优势
- Python数模笔记-PuLP库(1)线性规划入门
- [leetcode] 72. 编辑距离(二维动态规划)
- linux ( crontab 定时任务命令)