最好用的数据存储Easy Save2讲解

转载:http://www.manew.com/thread-100109-1-1.html
 
今天抽时间学习了“Easy Save2”插件,版本v2.6.3  我个人觉得这个插件是做数据存取最好的插件~~可以取代PlayerPrefs。
它不仅可以直接存取PlayerPrefs支持的int、float、string、bool
还包括下图中所有类型

 

如果不指定位置,数据会存储在Application.persistentDataPath里
你也可以指定它的存储位置 类似下面这样
[C#] 纯文本查看 复制代码
 
1
2
ES2.Save(data, "C:/Users/User/myFile.txt");    // 这里myFile.txt只存储data
ES2.Save(transform.position, "C:/Users/User/myFile.txt?tag=myPosition");   // 在myFile.txt文件里插入key:myPosition对应value:transform.position
transform.position = ES2.Load<Vector3>("C:/Users/User/myFile.txt?tag=myPosition");  //从myFile.txt文件里读取key:myPosition的value
也可以存储到web
[C#] 纯文本查看 复制代码
 
01
02
03
04
05
06
07
08
09
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
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<MeshFilter>().mesh = web.Load<Mesh>(tag);
    }
}
而且在存储和读取基本都是一条命令搞定,很方便。当然数据都是经过加密存储的
需要注意的是在存储Texture时,需要把图片类型改成“Advanced”,并勾选“Read/Write Enabled”
下面给大家分享下我测试的几个类型
[C#] 纯文本查看 复制代码
 
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
  
  
public class SaveTest : MonoBehaviour {
  
    public Image image;
  
    public void Save()
    {
        ES2.Save(123, "IntData");
  
        ES2.Save(1.23f, "FloatData");
  
        ES2.Save(true, "BoolData");
  
        ES2.Save("abc", "StringData");
  
        ES2.Save(new Vector3(10, 20, 30), "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, "TransformData");
  
        // 存储数组
        int[] intArray = new int[3] { 3, 2, 1 };
        ES2.Save(intArray, "IntArrayData");
  
        // 存储集合
        List<string> stringList = new List<string>();
        stringList.Add("stringlist1");
        stringList.Add("stringlist2");
        stringList.Add("stringlist3");
        ES2.Save(stringList, "StringListData");
  
        // 存储字典
        Dictionary<int, string> stringDict = new Dictionary<int, string>();
        stringDict.Add(1, "a");
        stringDict.Add(2, "b");
        ES2.Save(stringDict, "StringDictData");
  
        // 存储栈
        Stack<string> stringStack = new Stack<string>();
        stringStack.Push("aaa");
        stringStack.Push("bbb");
        ES2.Save(stringStack, "StringStackData");
  
        ES2.SaveImage(image.sprite.texture, "MyImage.png");
    }
  
    public void Load()
    {
        int loadInt = ES2.Load<int>("IntData");
        Debug.Log("读取的int:" + loadInt);
  
        float loadFloat = ES2.Load<float>("FloatData");
        Debug.Log("读取的float:" + loadFloat);
  
        bool loadBool = ES2.Load<bool>("BoolData");
        Debug.Log("读取的bool:" + loadBool);
  
        string loadString = ES2.Load<string>("StringData");
        Debug.Log("读取的string:" + loadString);
  
        Vector3 loadVector3 = ES2.Load<Vector3>("Vector3Data");
        Debug.Log("读取的vector3:" + loadVector3);
  
        Transform loadTransform = ES2.Load<Transform>("TransformData");
        Debug.Log("读取的transform: 坐标" + loadTransform.localPosition + " 缩放" + loadTransform.localScale);
  
        // 读取数组格式存储
        int[] loadIntArray = ES2.LoadArray<int>("IntArrayData");
        foreach (int i in loadIntArray)
        {
            Debug.Log("读取的数组:" + i);
        }
  
        // 读取集合格式存储
        List<string> loadStringList = ES2.LoadList<string>("StringListData");
        foreach (string s in loadStringList)
        {
            Debug.Log("读取的集合数据:" + s);
        }
  
        // 读取字典格式存储
        Dictionary<int, string> loadStringDict = ES2.LoadDictionary<int, string>("StringDictData");
        foreach (var item in loadStringDict)
        {
            Debug.Log("读取的字典数据: key" + item.Key + " value" + item.Value);
        }
  
        Stack<string> loadStringStack = ES2.LoadStack<string>("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));
        image.sprite = temp;
  
        // 判断是否有该存储key
        Debug.Log(ES2.Exists("IntData"));
  
        // 删除存储key
        ES2.Delete("IntData");
    }
  
  
}

 

附上下载链接:链接:http://pan.baidu.com/s/1gfAd0uJ 密码:3qd1   
posted @ 2017-10-17 15:52  三页菌  阅读(447)  评论(0编辑  收藏  举报