Unity3D 本地数据持久化几种方式
下面介绍几种 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>
Unity3D 本地数据持久化几种方式的更多相关文章
- ios网络学习------4 UIWebView的加载本地数据的三种方式
ios网络学习------4 UIWebView的加载本地数据的三种方式 分类: IOS2014-06-27 12:56 959人阅读 评论(0) 收藏 举报 UIWebView是IOS内置的浏览器, ...
- iOS --- UIWebView的加载本地数据的三种方式
UIWebView是IOS内置的浏览器,可以浏览网页,打开文档 html/htm pdf docx txt等格式的文件. safari浏览器就是通过UIWebView做的. 服务器将MIM ...
- Linux就这个范儿 第15章 七种武器 linux 同步IO: sync、fsync与fdatasync Linux中的内存大页面huge page/large page David Cutler Linux读写内存数据的三种方式
Linux就这个范儿 第15章 七种武器 linux 同步IO: sync.fsync与fdatasync Linux中的内存大页面huge page/large page David Cut ...
- python爬虫---爬虫的数据解析的流程和解析数据的几种方式
python爬虫---爬虫的数据解析的流程和解析数据的几种方式 一丶爬虫数据解析 概念:将一整张页面中的局部数据进行提取/解析 作用:用来实现聚焦爬虫的吧 实现方式: 正则 (针对字符串) bs4 x ...
- Solr 删除数据的几种方式
原文出处:http://blog.chenlb.com/2010/03/solr-delete-data.html 有时候需要删除 Solr 中的数据(特别是不重做索引的系统中,在重做索引期间).删除 ...
- SparkStreaming获取kafka数据的两种方式:Receiver与Direct
简介: Spark-Streaming获取kafka数据的两种方式-Receiver与Direct的方式,可以简单理解成: Receiver方式是通过zookeeper来连接kafka队列, Dire ...
- SparkStreaming与Kafka,SparkStreaming接收Kafka数据的两种方式
SparkStreaming接收Kafka数据的两种方式 SparkStreaming接收数据原理 一.SparkStreaming + Kafka Receiver模式 二.SparkStreami ...
- 【代码笔记】iOS-向服务器传JSON数据的两种方式
一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...
- .NET MVC控制器向视图传递数据的四种方式
.NET MVC控制器向视图传递数据的四种方式: 1.ViewBag ViewBag.Mvc="mvc"; 2.ViewData ViewBag["Mvc"] ...
随机推荐
- 『动善时』JMeter基础 — 19、JMeter配置元件【随机变量】
目录 1.随机变量介绍 2.随机变量界面详解 3.随机变量的使用 (1)测试计划内包含的元件 (2)线程组界面内容 (3)随机变量界面内容 (4)HTTP请求界面内容 (5)查看结果 1.随机变量介绍 ...
- Office·Word高级·VBA基础概念语法
阅文时长 | 5.21分钟 字数统计 | 1823字符 『Office·Word高级·VBA基础概念语法』 编写人 | SCscHero 编写时间 | Monday, June 29, 2020 文章 ...
- SpringBoot整合shiro系列-SpingBoot是如何将shiroFilter注册到servlet容器中的
一.先从配置类入手,主要是@Bean了一个ShiroFilterFactoryBean: @Data @Configuration @Slf4j @EnableConfigurationPropert ...
- [bug] IDEA Maven 项目 Module 不加粗,无法编译
参考 https://blog.csdn.net/qq_42479920/article/details/102859244
- RHCE脚本题目详解
目录 RHCE脚本题目详解 题目一 shell脚本之if语句实现: shell脚本之case语句实现: 题目二 实现 测试 解析 写在后面 RHCE脚本题目详解 题目一 在system1上创建一个名为 ...
- 本文介绍使用windows系统自带的远程桌面mstsc连接Centos 7.x远程桌面的基本方法。
本文介绍使用windows系统自带的远程桌面mstsc连接Centos 7.x远程桌面的基本方法. 一.前言 我希望用windows远程访问centos图形界面.xmanager连接centos远程桌 ...
- Linux基本原则
Bash特性 Shell shell(外壳),广义的shell可以理解为是用户的工作环境,在windows看来桌面就是一个shell,在linux看来终端就是shell 常见的shell有两种,一种是 ...
- nvm、nrm、npm 安装和使用详解
一.nvm的安装和使用 nvm全称Node Version Manager是 Nodejs 版本管理器,它让我们能方便的对 Nodejs 的版 本进行切换. nvm 的官方版本只支持 Linux ...
- 任务相关的API函数-uxTaskGetSystemState
uxTaskGetSystemState:此函数用于获取系统中所有的任务状态,每个任务的状态信息保存在一个TaskStatus_t类型的结构体里面.要使用此函数必须把 configUSE_TRACE_ ...
- C#中的数据结构
Array 连续性的内存空间 快速定位查找元素,随机访问性强,查找效率高 已知的统一的元素类型,减小运行时开销 固定长度,不能再新增元素 ArrayList 连续性的内存空间 快速定位查找元素,随机访 ...