EasySave数据存储插件

发表于2017-02-10
评论0 4.5k浏览
在实际开发中我们需要储存各种类型的数据,比较常见的方式就是用xml、json再者就是二进制的方式,以上这几种方式都非常方便,不过带来的问题就是储存的数据都是明文,要想保护数据还需要另外加密什么的有点繁琐,今天无意中看到Easy
Save这个插件,于是就用了一些时间研究了一下:Easy
Save这个插件大小也就几百k,所以导入工程也不用担心会造成工程臃肿的问题,而且Easy Save支持的储存类型有很多(如图):

Easy Save存储和读取基本都是一条命令就能搞定非常方便,而且数据都是经过加密存储所以也不用担心数据泄密的问题。下面举个例子说明一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//数据存储路径(Easy Save的默认储存位置为:Application.persistentDataPath,为了方便我们可以给它指定储存路径)
private string dataPath = "F:/test/Demo.txt?tag=Position";
  
void OnGUI()
{
   if (GUI.Button(new Rect(0, 0, 100, 100), "储存物体的位置"))
   {
       //在Demo.txt文件里插入key:Position对应value:transform.position
       ES2.Save(transform.position, dataPath);
   }
  
   if (GUI.Button(new Rect(0, 0, 100, 100), "读取物体的位置"))
   {
       //读取key:Position对应的value
       Vector3 loadVector3 = ES2.Load(dataPath);
       Debug.Log("读取的vector3:" + loadVector3);
   }
}


Easy Save不仅可以保存到本地而且还能保存到web:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public IEnumerator UploadMesh(Mesh mesh, string tag)
{
   // Create a URL and add parameters to the end of it.
   string myURL = "http://www.server.com/ES2.php";
   myURL += "?webfilename=myFile.txt&webusername=user&webpassword=pass";
  
   // Create our ES2Web object.
   ES2Web web = new ES2Web(myURL + "&tag=" + tag);
  
   // Start uploading our data and wait for it to finish.
   yield return StartCoroutine(web.Upload(mesh));
  
   if (web.isError)
   {
       // Enter your own code to handle errors here.
       Debug.LogError(web.errorCode + ":" + web.error);
   }
}
  
public IEnumerator DownloadMesh(string tag)
{
   // Create a URL and add parameters to the end of it.
   string myURL = "http://www.server.com/ES2.php";
   myURL += "?webfilename=myFile.txt&webusername=user&webpassword=pass";
  
   // Create our ES2Web object.
   ES2Web web = new ES2Web(myURL + "&tag=" + tag);
  
   // Start downloading our data and wait for it to finish.
   yield return StartCoroutine(web.Download());
  
   if (web.isError)
   {
       // Enter your own code to handle errors here.
       Debug.LogError(web.errorCode + ":" + web.error);
   }
   else
   {
       // We could save our data to a local file and load from that.
       web.SaveToFile("myFile.txt");
  
       // Or we could just load directly from the ES2Web object.
       this.GetComponent().mesh = web.Load(tag);
   }
}

 
下面是类型测试Demo(工程下载地址在文章末尾):
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
 
public class Test : MonoBehaviour
{
   ///
   /// 要存储的图片
   ///
   public Image image;
   ///
   /// 要显示的图片
   ///
   public Image showImage;
 
   ///
   /// 数据存储路径(Easy Save的默认储存位置为:Application.persistentDataPath,为了方便我们可以给它指定储存路径)
   ///
   private string dataPath = "F:/test/Demo.txt?tag=";
 
   ///
   /// 存储数据
   ///
   private void SaveData()
   {
       ES2.Save(123, dataPath+"IntData");
 
       ES2.Save(1.23f, dataPath + "FloatData");
 
       ES2.Save(true, dataPath + "BoolData");
 
       ES2.Save("abc", dataPath + "StringData");
 
       ES2.Save(new Vector3(10, 20, 30), dataPath + "Vector3Data");
 
       ///< 存储transform
       GameObject go = new GameObject();
       go.transform.localPosition = new Vector3(10, 20, 30);
       go.transform.localScale = new Vector3(3, 3, 3);
       ES2.Save(go.transform, dataPath + "TransformData");
 
       ///< 存储数组
       int[] intArray = new int[3] { 3, 2, 1 };
       ES2.Save(intArray, dataPath + "IntArrayData");
 
       ///< 存储集合
       List stringList = new List();
       stringList.Add("stringlist1");
       stringList.Add("stringlist2");
       stringList.Add("stringlist3");
       ES2.Save(stringList, dataPath + "StringListData");
 
       ///< 存储字典
       Dictionary stringDict = new Dictionary();
       stringDict.Add(1, "a");
       stringDict.Add(2, "b");
       ES2.Save(stringDict, dataPath + "StringDictData");
 
       ///< 存储栈
       Stack stringStack = new Stack();
       stringStack.Push("aaa");
       stringStack.Push("bbb");
       ES2.Save(stringStack, dataPath + "StringStackData");
 
       ES2.SaveImage(image.sprite.texture, "MyImage.png");
   }
 
   ///
   /// 加载数据
   ///
   private void LoadData()
   {
       int loadInt = ES2.Load(dataPath + "IntData");
       Debug.Log("读取的int:" + loadInt);
 
       float loadFloat = ES2.Load(dataPath + "FloatData");
       Debug.Log("读取的float:" + loadFloat);
 
       bool loadBool = ES2.Load(dataPath + "BoolData");
       Debug.Log("读取的bool:" + loadBool);
 
       string loadString = ES2.Load(dataPath + "StringData");
       Debug.Log("读取的string:" + loadString);
 
       Vector3 loadVector3 = ES2.Load(dataPath + "Vector3Data");
       Debug.Log("读取的vector3:" + loadVector3);
 
       Transform loadTransform = ES2.Load(dataPath + "TransformData");
       Debug.Log("读取的transform: Position++" + loadTransform.localPosition + " +++Scale++" + loadTransform.localScale);
 
       ///< 读取数组格式存储
       int[] loadIntArray = ES2.LoadArray(dataPath + "IntArrayData");
       foreach (int i in loadIntArray)
       {
           Debug.Log("读取的数组:" + i);
       }
 
       ///< 读取集合格式存储
       List loadStringList = ES2.LoadList(dataPath + "StringListData");
       foreach (string s in loadStringList)
       {
           Debug.Log("读取的集合数据:" + s);
       }
 
       ///< 读取字典格式存储
       Dictionary loadStringDict = ES2.LoadDictionary(dataPath + "StringDictData");
       foreach (var item in loadStringDict)
       {
           Debug.Log("读取的字典数据: key" + item.Key + " value" + item.Value);
       }
 
       Stack loadStringStack = ES2.LoadStack(dataPath + "StringStackData");
       foreach (string ss in loadStringStack)
       {
           Debug.Log("读取的栈内数据:" + ss);
       }
 
       ///< 读取纹理
       Texture2D tex = ES2.LoadImage("MyImage.png");
       Sprite temp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0, 0));
       showImage.sprite = temp;
   }
 
   ///
   /// 删除数据
   ///
   private void DeleteData()
   {
       ///< 判断是否有该存储key
       if (ES2.Exists(dataPath + "IntData"))
       {
           Debug.Log(ES2.Exists(dataPath + "IntData"));
           ///< 删除存储key
           ES2.Delete(dataPath + "IntData");
       }
   }
 
   void OnGUI()
   {
       if (GUI.Button(new Rect(0, 0, 100, 100), "储存数据"))
       {
           SaveData();
       }
 
       if (GUI.Button(new Rect(0, 100, 100, 100), "读取数据"))
       {
           LoadData();
       }
 
       if (GUI.Button(new Rect(0, 200, 100, 100), "删除数据"))
       {
           DeleteData();
       }
   }
}


存储图片时需要把图片类型设置成“Advanced”并且勾选可读写的(如图):
工程下载请到原文!!!

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

0个评论