今天抽时间学习了“Easy Save2”插件,版本v2.6.3 我个人觉得这个插件是做数据存取最好的插件~~可以取代PlayerPrefs。
它不仅可以直接存取PlayerPrefs支持的int、float、string、bool
还包括下图中所有类型
如果不指定位置,数据会存储在Application.persistentDataPath里
你也可以指定它的存储位置 类似下面这样
1 2 3 |
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
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<MeshFilter>().mesh = web.Load<Mesh>(tag); } } |
而且在存储和读取基本都是一条命令搞定,很方便。当然数据都是经过加密存储的
需要注意的是在存储Texture时,需要把图片类型改成“Advanced”,并勾选“Read/Write Enabled”
下面给大家分享下我测试的几个类型
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 |
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"); } } |
附上下载链接:https://pan.baidu.com/s/1o8dMZpg 密码:sq66 插件仅用于学习,禁止商用,为了维护您的个人权益,请支持正版
- 本文固定链接: http://www.u3d8.com/?p=1000
- 转载请注明: 网虫虫 在 u3d8.com 发表过