Unity插件学习(五) ------ 本地存储Easy Save3
文章目录
- 一.前言及下载地址
- 二.功能介绍
- 三.使用方法
- 1.[Easy Save3存储支持的类型](https://docs.moodkie.com/easy-save-3/es3-supported-types/)
- 2.设置
- 3.Keys, Paths and Locations
- File
- PlayerPrefs
- Resources
- Memory
- 5.加密
- 6.保存并加载字符串和字节到文件
- 7.保存和加载gameobject和Prefabs
- 8.备份
- 9.使用ES3File缓存
- 10.保存和加载Image、audio
- 11.Saving and Loading from Resources
- 12.使用es3类型控制序列化
- 13.与其他存储api集成
- 14.电子表格
一.前言及下载地址
公司小项目,经常要保存数据到本地,unity自带API PlayerPrefs只能简单的存储基本类型,因此找到这个比较好用的插件Easy Save
插件以及demo下载链接:https://download.csdn.net/download/dengshunhao/10765006
官网 : https://docs.moodkie.com/product/easy-save-3/
二.功能介绍
链接:https://docs.moodkie.com/easy-save-3/es3-guides/features/
1.保存及获取
ES3.Save / ES3.Load / ES3.LoadIntoSave and Load from ResourcesSave and Load Strings and Bytes to File
3.加密
4.缓存
5.备份
6.保存并加载GameObjects和Prefab Instances
7.保存及加载图片、音频
8.File IO
Check if Key, File or Directory ExistsES3.KeyExists / ES3.FileExists / ES3.DirectoryExistsFind Keys, Files and DirectoriesES3.GetKeys / ES3.GetFiles / ES3.GetDirectoriesDelete Keys, Files and DirectoriesRename FilesCopy Files
三.使用方法
1.Easy Save3存储支持的类型
Primitive types(原始类型)
Structs(结构体)
Enums(枚举)
Components/MonoBehaviours(组件)
ScriptableObjects
Non-abstract classes with a parameterless constructor(具有无参数构造函数的非抽象类)
Arrays, Lists, Dictionaries, Queues, Stacks and HashSets of supported types(数组、列表、字典、队列、堆栈和受支持类型的散列集)
Non-generic(非泛型)
Nice,我想要的都有了,特别喜欢Dictionariesn能存储,毕竟先前自己写转成List,再读取蛮麻烦的
2.设置
修改默认设置:
这里面的属性也比较好理解,自己看看吧,工具也挺实用的
运行时修改:
最简单的保存方法允许您提供一个ES3Settings对象作为参数来覆盖默认设置。
只需创建一个新的ES3Settings对象,更改它的变量,并将其作为参数提供给您的方法调用,以在运行时覆盖默认设置。
// Create a new ES3Settings to enable encryption.
var settings = new ES3Settings(ES3.EncryptionType.AES, "myPassword");
// Change the save location to PlayerPrefs.
settings.saveLocation = ES3.Location.PlayerPrefs;// Use the ES3Settings object to encrypt data and save to PlayerPrefs.
ES3.Save<Transform>("myTransform", this.transform, settings);
补充下参数说明:
属性 | 意义 |
---|---|
location | The storage location where we wish to store data by default. |
path | The path this ES3Settings object points to, if any. |
encryptionType | The type of encryption to use when encrypting data, if any. |
encryptionPassword | The password to use to encrypt the data if encryption is enabled. |
directory | The default directory in which to store files when using the File save location, and the location which relative paths should be relative to. |
format | The format we should use when serializing and deserializing data. |
bufferSize | Any stream buffers will be set to this length in bytes. |
encoding | What text encoding to use when writing and reading text from a file. |
3.Keys, Paths and Locations
键允许您将多个数据片段存储到同一个文件中。例如:
// These three values are stored to the default file.
ES3.Save<int>("myKey1", 1);
ES3.Save<int>("myKey2", 2);
ES3.Save<int>("myKey3", 3);// These three values are stored to the file "myFile.es3".
ES3.Save<int>("myKey1", 1, "myFile.es3");
ES3.Save<int>("myKey2", 2, "myFile.es3");
ES3.Save<int>("myKey3", 3, "myFile.es3");// These three values are stored to different files.
ES3.Save<int>("myKey1", 1);
ES3.Save<int>("myKey2", 2, "myFile.es3");
ES3.Save<int>("myKey3", 3, "anotherFile.es3");
如果一个文件已经包含了给定的键,文件中的值将被覆盖。例如:
// This adds the key "myKey" to the file with a value of '1'.
ES3.Save<int>("myKey", 1);
// This overwrites "myKey", so the value is now '2'.
ES3.Save<int>("myKey", 2);
// This overwrites "myKey" again, so the value is now '3'.
ES3.Save<int>("myKey", 3);
支持相对路径和绝对路径。如果文件或目录不存在,将创建它。
// Save a value to the default file in the default save location.
ES3.Save<int>("myKey", myValue);// Save a value to a file named "myFile.es3" in the default save location.
ES3.Save<int>("myKey", myValue, "myFile.es3");// Save a value to a file named "myFile.es3" in a sub-directory called "myFolder" in the default save location.
ES3.Save<int>("myKey", myValue, "myFolder/myFile.es3");// Save a value to a file named "myFile.es3" in an absolute folder.绝对路径
ES3.Save<int>("myKey", myValue, "C:/Users/User/Documents/myFile.es3);
您可以在默认设置中指定存储位置,或者提供ES3Settings对象作为参数。
File
将数据存储到文件系统中的文件中。
默认的文件目录是Unity的应用程序。可以得到使用Debug.Log(Application.persistentDataPath)的精确位置。
您还可以选择使用应用程序。通过更改ES3Settings来使用dataPath。目录变量到ES3.Directory。DataPath,但只能保证可从编辑器写入。
文件在WebGL中不受支持,并且将自动默认为PlayerPrefs,不管位置设置为什么。
PlayerPrefs
将数据存储在Unity的PlayerPrefs中,后者通常将数据存储在注册表或独立存储中。
这是为WebGL等平台提供的,这些平台不支持保存到文件,但是很少需要手动将保存位置设置为PlayerPrefs。
在WebGL中,PlayerPrefs有1MB的限制。
Resources
Memory
内存保存位置只能在特殊情况下使用,例如ES3File。
5.加密
Easy Save目前支持AES加密,使用128位密钥,可以在默认设置中启用,也可以使用ES3Settings对象作为参数。
默认情况下,加密是禁用的。
由于导出限制,一些应用程序商店要求您声明正在使用加密。要了解更多相关信息,你应该联系相关的app store
// Create a new ES3Settings to enable encryption.
var settings = new ES3Settings(ES3.EncryptionType.AES, "myPassword");// Use the ES3Settings object to encrypt data.
ES3.Save<Transform>("myTransform", this.transform, settings);
// Use the ES3Settings object to load and decrypt the data.
ES3.LoadInto<Transform>("myTransform, this.transform, settings");
6.保存并加载字符串和字节到文件
Easy Save允许您使用ES3将字符串和字节直接保存到文件中。SaveRaw ES3.AppendRaw。
如果您想将数据写入文件,而文件的格式与容易保存的用途不同,那么这是非常有用的。
您还可以使用ES3以字符串或字节数组的形式读取文件。LoadRawString或ES3.LoadRawBytes。
保存:
// Create a string which represents three lines in a text file.
string myString = "Line1\nLine2\nLine3";// Save the string as a file.
ES3.SaveRaw(myString, "myFile.txt");// Append another line to the file.
ES3.AppendRaw("\nLine4", "myFile.txt");// Now load the file back.
// This string will be "Line1\nLine2\nLine3\nLine4"
myString = ES3.LoadRawString("myFile.txt");
获取:
// Create some bytes which we wish to store to a file.
byte[] myBytes = GetBytes();// Save the bytes as a file.
ES3.SaveRaw(myBytes, "myFile.bytes");// Append more bytes to the file.
ES3.AppendRaw(GetMoreBytes(), "myFile.bytes");// Now load the file back as a byte array.
myBytes = ES3.LoadRawBytes("myFile.bytes");
7.保存和加载gameobject和Prefabs
手动保存和加载gameobject:
// Save a GameObject.
ES3.Save<GameObject>("myGameObject", go);// Load a GameObject, automatically assigning it to an existing
// GameObject if one exists, or create a new GameObject if not.
ES3.Load<GameObject>("myGameObject");// Or we can choose what instance we load the data into.
ES3.LoadInto<GameObject>("myGameObject", go);
这将保存和加载以下内容:
- 层,标签,名称和hideFlags。
- 本地支持的类型列表中的组件,或者使用ES3Type手动支持的组件。
- 对于GameObject的每个子对象,上述所有内容都适用。
手动保存和加载Prefabs:
保存和加载预置实例:
- 启用Easy Save for Prefab
- 通过右键单击它并选择Enable Easy Save for预设。
- 可以使用ES3.Save保存。
- 要加载,请使用ES3。负载< GameObject >或ES3.LoadInto < GameObject >。
- 加载时,如果加载的实例不存在,Easy Save将创建预置实例。
8.备份
有时候创建保存文件的备份是很有用的,例如,如果您希望在罕见的情况下恢复保存文件,比如由于硬件故障导致数据损坏,或者文件被篡改。
你可以用ES3来做这个。CreateBackup,胡状。还原备份以恢复备份。备份是通过复制文件并给它一个.bak扩展名来创建的。
如果备份已经存在,它将被覆盖,因此您需要确保在创建新备份之前不需要旧备份。
还要注意,恢复备份将覆盖它是备份的文件。
// Save some data and then make a backup.
ES3.Save<int>("myKey", "myFile.es3");
ES3.CreateBackup("myFile.es3");try{myInt = ES3.Load<int>("myInt", "myFile.es3");}catch{if(ES3.RestoreBackup("myFile.es3"))Debug.Log("Backup restored.");elseDebug.Log("Backup could not be restored as no backup exists.");}
9.使用ES3File缓存
使用ES3File缓存文件可以显著提高性能。这样做的好处是:
- 访问密钥要快得多。
- 覆盖键要快得多,需要的内存也少得多。
- 只需要一次写入存储。
- 该文件将存储在内存中,但在大多数情况下,内存使用是可以忽略不计的。
使用ES3File: - 当您使用 ES3File Constructor时,文件将从存储库加载到ES3File中。
- 要保存和加载密钥,请使用 ES3File.Save, ES3File.Load 和ES3File.LoadInto方法。
- 若要将ES3File提交回存储区,请调用 ES3File.Sync 。这将用缓存的数据覆盖存储中的文件。
- 要将ES3File转换为字节数组或字符串,请使用 ES3File.LoadRawBytes and ES3File.LoadRawString方法。
public class SaveLoadCache : MonoBehaviour
{public static ES3File file;public void Start(){// Create a new ES3File. This automatically loads// the data from myFile.es3 into the ES3File.file = new ES3File("myFile.es3");}public void OnApplicationQuit(){// Commit our ES3File to storage when the application quits.file.Sync();}// We can call this method whenever we want to save.public static void Save(){file.Save<string>("myName", this.name);file.Save<Transform>("myTransform", transform);}// We can call this method whenever we want to load.public static void Load(){this.name = file.Load<string>("myName", "defaultName");// We can check whether a key exists in the ES3File before loading it.if(file.KeyExists("myTransform"))file.LoadInto<Transform>("myTransform", transform);}
}
10.保存和加载Image、audio
Image:
// Take a screenshot.
var texture = new Texture2D(Screen.width, Screen.height);
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();// Save the screenshot as a PNG file.
ES3.SaveImage(texture, "screenshot.png");// Save the screenshot as a JPG file.
ES3.SaveImage(texture, "screenshot.jpg");// Load a Texture2D from a PNG file.
var texture = ES3.LoadImage("myImage.png");
// Apply the Texture2D to the material on this object.
GetComponent<Renderer>.material.mainTexture = texture;// Get the bytes of a PNG file from an external cloud service.
byte[] bytes = CloudService.GetFileBytes("file.png");
// Turn these bytes into a Texture2D.
var texture = ES3.LoadImage(bytes);
audio:
加载:
MP3文件只支持移动设备,Ogg Vorbis文件只支持独立平台。
除了WebGL之外,所有平台都支持WAV、XM、IT、MOD和S3M文件。
由于此方法需要文件访问,所以WebGL不支持此方法。
如果文件不存在,将抛出FileNotFoundException。在这种情况下,您可以使用ES3。FileExists用于在加载前检查数据是否存在。
保存:
目前不可能将音频保存到压缩格式,因为Unity缺乏这样做所需的编码器。
但是,可以使用普通ES3以轻松保存的格式保存和加载AudioClip。保存并胡状。加载方法。由于数据未压缩,文件大小将大于压缩格式。
// Get the AudioSource we want to use to play our AudioClip.
var source = this.GetComponent<AudioSource>();
// Load an AudioClip from the streaming assets folder into our source.
source.clip = ES3.LoadAudio(Application.streamingAssetsPath + "/AudioFile.wav");
// Play the AudioClip we just loaded using our AudioSource.
source.Play();// Get the AudioSource containing our AudioClip.
var source = this.GetComponent<AudioSource>();
// Save an AudioClip in Easy Save's uncompressed format.
ES3.Save<AudioClip>("myAudio", source.clip);
// Load the AudioClip back into the AudioSource and play it.
source.clip = ES3.Load<AudioClip>("myAudio");
source.Play();
11.Saving and Loading from Resources
Load:
要从资源加载,必须在默认设置中将位置设置为资源,或者使用ES3Settings对象。
文件必须具有扩展名.bytes,以便能够从参考资料中加载它
// Create an ES3Settings object to set the storage location to Resources.
var settings = new ES3Settings();
settings.location = ES3.Location.Resources;// Load from a file called "myFile.bytes" in Resources.
var myValue = ES3.Load<Vector3>("myFile.bytes", settings);// Load from a file called "myFile.bytes" in a subfolder of Resources.
var myValue = ES3.Load<Vector3>("myFolder/myFile.bytes");
Save:
Easy Save只能从编辑器中保存到Resources文件夹,因为在构建Unity应用程序时,Resources文件夹不存在。
如果希望将数据保存到资源中,以便稍后加载,则文件必须以扩展名.bytes结束。
要直接保存到资源,可以使用应用程序。获取路径的dataPath + “/Resources/”。但是,您需要退出Play模式并调用AssetDatabase.Refresh()或手动刷新项目才能从该文件加载。
或者,您可以将数据保存到您选择的文件夹中,然后将其拖放到Resources文件夹中。
ES3.Save<int>("myKey", 123, Application.dataPath+"/Resources/myFile.bytes");
AssetDatabase.Refresh();
12.使用es3类型控制序列化
ES3Type脚本告诉我们如何保存类型的字段和属性,以及如何保存它们。这可以自动创建,并在必要时进行修改。
一旦创建了脚本,Easy Save就会自动找到它,并使用它序列化和反序列化您的类型。您不需要创建ES3Type的实例。
创建一个ES3Type:
可以通过转到窗口> Easy Save 3来创建ES3Type,并选择Types选项卡。
从这里,您可以从列表中选择您的类型,并选择希望保存的字段和属性。
- 注意,所有字段和属性都将显示,而不管它们是否用[Serializable]属性标记。
有时候,试图保存一个属性可能会产生意想不到的效果。 - 因此建议您在选择属性之前确保序列化它是安全的。
手动修改ES3Type:
在某些情况下,您需要修改ES3Type。例如:
当类型没有无参数的构造函数时。 - 当您需要调用一个方法来访问您正在保存/加载的变量时。
- 可以通过修改Types窗格中生成的ES3Type脚本的写、读和读入方法来实现这一点。ES3Type脚本可以在/Assets/Easy Save 3/Types/中找到。
注意,一旦您手动修改了ES3Type文件,那么在Types面板中所做的任何更改都会覆盖您的手动修改。
写:
protected override void WriteComponent(object obj, ES3Writer writer)
{var instance = (MyScript)obj;writer.WriteProperty<int>("points", instance.points);writer.WriteProperty<float>("speed", instance.speed);writer.WriteProperty<Transform>("partner", instance.partner);
}
读:
protected override void ReadComponent<T>(ES3Reader reader, object obj)
{var instance = (MyScript)obj;foreach(string propertyName in reader.Properties){switch(propertyName){case "points":instance.points = reader.Read<int>();break;case "speed":instance.speed = reader.Read<float>();break;case "partner":instance.partner = reader.Read<Transform>();break;default:reader.Skip();break;}}
}
13.与其他存储api集成
有时需要将Easy Save的API与其他服务的存储API集成。例如,支持控制台,或与云存储插件集成。
这可以通过使用ES3File写入内存而不是本地存储,并以字节数组或字符串的形式检索ES3File的内容来实现。
以字符串或字节数组的形式保存:
// Create a new ES3File, providing a false parameter.
var es3file = new ES3File(false);// Save your data to the ES3File.
es3File.Save<Transform>("myTransform", this.transform);
es3File.Save<string>("myName", myScript.name);
// etc ...// Get the ES3File as a string.
string fileAsString = es3File.LoadRawString();
// Or get it as a byte array.
byte[] fileAsByteArray = es3File.LoadRawBytes().
从字符串或字节数组加载:
存储API应该允许您以字符串或字节数组的形式检索数据。
然后,可以将这个字符串或字节数组加载到ES3File中并从中读取。
// If we're loading from a byte array, simply provide it as a parameter.
var es3file = new ES3File(fileAsByteArray, false);// If we're loading as a string, we need to convert it to a byte array first.
var es3file = new ES3File((new ES3Settings()).encoding.GetBytes(fileAsString), false);// Load the data from the ES3File.
es3File.LoadInto<Transform>("myTransform", this.transform);
myScript.name = es3File.Load<string>("myName");
// etc ...
14.电子表格
使用ES3Spreadsheet, Easy Save能够创建电子表格并以CSV格式存储,所有流行的电子表格软件都支持这种格式,包括Excel、OSX数字和OpenOffice。
Save:
var sheet = new ES3Spreadsheet();
// Add data to cells in the spreadsheet.
for(int col=0; col<10; col++)for(int row=0; row<8; row++)sheet.SetCell<string>(col, row, "someData");
sheet.Save("mySheet.csv");
如果要将数据追加到现有的电子表格,请将电子表格的追加变量设置为true。电子表格中的任何行都将被添加到保存到的行末尾。
Load:
// Create a blank ES3Spreadsheet.
var sheet = new ES3Spreadsheet();
sheet.Load("spreadsheet.csv");
// Output the first row of the spreadsheet to console.
for(int col=0; col<sheet.ColumnCount; col++)Debug.Log(sheet.GetCell<int>(col, 0));
最新版的easySave3运行会报错,按照以下修改即可:
Unity插件学习(五) ------ 本地存储Easy Save3相关推荐
- Prometheus 学习之——本地存储 TSDB
Prometheus 学习之--本地存储 TSDB 文章目录 Prometheus 学习之--本地存储 TSDB 前言 一.TSDB 核心概念 二.详细介绍 1.block 1)chunks 2)in ...
- JS9day(BOM对象模型,setTimeout定时器,JS单线程执行机制,location对象,swiper插件,localStorage本地存储,购物车案例升级版,学习信息案例(本地存储))
文章目录 BOM简介 定时器-延时函数 5秒关闭广告案例 递归模拟setInterval函数 两种定时器对比 JS 执行机制 location对象 navigator对象 histroy对象(了解) ...
- html5保存资源本地,html5之Localstorage本地存储
题外话 今天把博客里面的内容,同步在github的issues中了.具体地址是:https://github.com/confidence68/blog/issues ,欢迎大家访问,给star. L ...
- uniapp结合腾讯云及时通信IM的聊天记录本地存储方案
uniapp结合腾讯云及时通信IM的聊天记录本地存储方案 UniApp 是一个跨平台的应用开发框架,可以使用 Vue.js 开发多端应用(如H5.小程序.App等).在 UniApp 中,可以使用 u ...
- Unity存档探索:PlayerPref、 Easy Save 2、Easy Save3
PlayerPref 非常粗暴,直接使用键值对的方法存储,很适合小游戏使用(flappy bird之类) 在Windows平台下,PlayerPrefs被存储在注册表的 HKEY_CURRENT_US ...
- 【Flutter】shared_preferences 本地存储 ( 简介 | 安装 shared_preferences 插件 | 使用 shared_preferences 流程 )
文章目录 一.shared_preferences 本地存储插件简介 二.安装 shared_preferences 插件 三.使用 shared_preferences 流程 四.完整代码示例 五. ...
- (五)HTML5本地存储——Web Storage
Web应用的发展,使得客户端存储使用得也越来越多,而实现客户端存储的方式则是多种多样.最简单而且兼容性最佳的方案是Cookie,但是作为真正的客户端存储,Cookie则存在很多致命伤.此外,在IE6及 ...
- HTML5 学习笔记(三)——本地存储(LocalStorage、SessionStorage、Web SQL Database)
一.HTML4客户端存储 B/S架构的应用大量的信息存储在服务器端,客户端通过请求响应的方式从服务器获得数据,这样集中存储也会给服务器带来相应的压力,有些数据可以直接存储在客户端,传统的Web技术中会 ...
- Unity插件之NGUI学习(4)—— 创建UI2DSprite动画
创建一个新的Scene.并按 Unity插件之NGUI学习(2)创建UI Root,并在UI Root的Camera下创建一个Panel. 然后在选中Panel,在菜单中选择NGUI->Crea ...
最新文章
- hue sqoop mysql_HUE中Oozie执行Sqoop
- 一晃居然已经停更半年了
- 使用OpenGL实现翻书动画
- WebSocket笔记(一) 初步认识
- 数据结构之图的存储结构:十字链表法
- Mysql错误1366的解决办法:Incorrect string value: '\xF0\x9F...' for column 'XXX' at row 1
- ubuntu下安装MySQL8.0
- ajax代码原理,关于Ajax的原理以及代码封装详解
- 抄底公式---预测黑马
- 文本分类实战--从TFIDF到深度学习CNN系列效果对比(附代码)
- 捕获asp.net下的未处理异常
- 构建最基础的Spring项目及所需要的jar包
- 实验吧-杂项-Only one file(多文件合并、firework多图层图片)
- vue改页面顶部浏览器标题栏图标、名称和地址栏详细教程
- InfofoIE浏览器的好助手(转)
- u盘写保护+计算机管理,U盘写保护的解决方法
- Vue中的自定义指令
- mac 谷歌浏览器 跨域访问
- 常见低压电器原理及电气符号(接触器、继电器、熔断器、断路器)基本原理及电气间隙与爬电距离
- 清华计算机自主招生试题,2017年清华大学自主招生笔试真题及答案汇总|2017自主招生笔试真题(清华大学)|清华大学2017年自主招生笔试真题...