Unity 二进制存储方法
2018-03-09 15:32
127 查看
在Manager类中
将文件存放在StreamingFile文件中
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[System.Serializable]
public struct Save
{
}
public void SaveGame()
{
saveByBin();
}
private void SaveByBin()
{
//序列化过程(将save转化为字节流)
//创建Save对象并保存当前游戏状态
Save save = CreateSaveGO();
//创建一个二进制格式化程序
BinaryFormatter bf = new BinaryFormatter();
//创建一个文件流
FileStream fileStream = File.Create(Application.dataPath + "/StreamingFile" + "/byBin.txt");
//用二进制格式化程序的序列化方法来序列化Save对象,参数:创建的文件流和需要序列化的对象
bf.Serialize(fileStream,save);
//关闭流
fileStream.Close;
if(File.Exists(Application.dataPath + "/StreamingFile" + "/byBin.txt")){
//若保存文件存在则执行
}
}
public void LoadGame()
{
LoadByBin();
}
private void LoadByBin()
{
if(!File.Exists(Application.dataPath + "/StreamingFile" + "/byBin.txt")){
return;
}
//反序列化过程
//创建一个二进制格式化程序
BinaryFormatter bf = new BinaryFormatter();
//打开一个文件流
FileStream fileStream = File.Open(Application.dataPath + "/StreamingFile" + "/byBin.txt",FileMode.Open);
//调用格式化程序的反序列化方法,将文件流转化为一个Save对象
Save save = (Save)bf.Deserialize(fileStream);
//关闭文件流
fileStream.Close;
SetGame(save);
}
private void SetGame(Save save)
{
foreach()
{
//清空原有数据
}
//重设数据
}
将文件存放在StreamingFile文件中
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[System.Serializable]
public struct Save
{
}
public void SaveGame()
{
saveByBin();
}
private void SaveByBin()
{
//序列化过程(将save转化为字节流)
//创建Save对象并保存当前游戏状态
Save save = CreateSaveGO();
//创建一个二进制格式化程序
BinaryFormatter bf = new BinaryFormatter();
//创建一个文件流
FileStream fileStream = File.Create(Application.dataPath + "/StreamingFile" + "/byBin.txt");
//用二进制格式化程序的序列化方法来序列化Save对象,参数:创建的文件流和需要序列化的对象
bf.Serialize(fileStream,save);
//关闭流
fileStream.Close;
if(File.Exists(Application.dataPath + "/StreamingFile" + "/byBin.txt")){
//若保存文件存在则执行
}
}
public void LoadGame()
{
LoadByBin();
}
private void LoadByBin()
{
if(!File.Exists(Application.dataPath + "/StreamingFile" + "/byBin.txt")){
return;
}
//反序列化过程
//创建一个二进制格式化程序
BinaryFormatter bf = new BinaryFormatter();
//打开一个文件流
FileStream fileStream = File.Open(Application.dataPath + "/StreamingFile" + "/byBin.txt",FileMode.Open);
//调用格式化程序的反序列化方法,将文件流转化为一个Save对象
Save save = (Save)bf.Deserialize(fileStream);
//关闭文件流
fileStream.Close;
SetGame(save);
}
private void SetGame(Save save)
{
foreach()
{
//清空原有数据
}
//重设数据
}
相关文章推荐
- NET二进制图片存储与读取的常见方法,iTextSharp添加图片生成PDF文件
- 近日探得用C++将二进制数据存储到XML文件的方法,因在研究时,未得到网上其他同仁的帮助(网上搜索了半天没有相关资料,只有DOTNET的),在这里不敢独享,给别的同仁在搜索时能够搜索到相关资料,也算是绵薄之力! )
- C#中将结构类型数据存储到二进制文件中方法
- NET二进制图片存储与读取的常见方法
- C#中将结构类型数据存储到二进制文件中方法
- 近日探得用C++将二进制数据存储到XML文件的方法,由于在研究时,未得到网上其他同仁的帮助(网上搜索了半天没有相关资料,只有.NET的),在这里不敢独享,给别的同仁在搜索时能够搜索到相关资料,也算是绵薄之力! )
- .net二进制图片存储与读取的常见方法
- .NET二进制图片存储与读取的常见方法
- NET二进制图片存储与读取的常见方法
- 用VC,VB进行图像数据(二进制大对象)存储数据库的方法
- unity - 二进制文件操作-存储与读取
- NET二进制图片存储与读取的常见方法
- .NET二进制图片存储与读取的常见方法
- NET二进制图片存储与读取数据库的常见方法
- NET二进制图片存储与读取的常见方法
- unity - 二进制文件操作-存储与读取
- Unity数据存储(二进制,xml,json)
- 在数据库中 存储图片 以及 在界面中显示图片(存储图片路径)- 这种方法相对与存储二进制文件好
- 计算机中二进制的存储方法
- Unity5.4 Assetbundles官方说明七(在AssetBundle中存储和加载二进制数据)