首先感谢三位大神的博客 :
http://blog.youkuaiyun.com/u013895270/article/details/45579033 http://www.cnblogs.com/sunxuchu/p/5484018.html http://www.cnblogs.com/plateFace/p/5170544.html
PlayerPrefs类本身不进行加密 ,保持数据的时候容易被修改破解。使用多少key值存储太过麻烦,通过集成类可以存储实例对象。在大神的基础上稍微修改一下集合而成,方便使用。
一,测试代码如下
public class User {
public string Name ;
public int Age;
public string Describe ;
public string dse ;
public string text ;
}
PlayerPrefs.DeleteAll();
User user = new User();
user.Name = "哈哈哈";
user.Age = 122;
user.Describe = "test ";
user.dse = "text";
user.text = "text";
PlayerPrefsExtend.Save("user", user);
user = null;
user = PlayerPrefsExtend.GetValue<User>("user");
Debug.Log("user name: " + user.Name);
Debug.Log("user Age: " + user.Age);
Debug.Log("user Describe: " + user.Describe);
二,存储读取实例对象
using UnityEngine;
using System.Collections;
using System;
using System.Reflection;
public static class PlayerPrefsExtend
{
public static void Save(string name, object o)
{
Type t = o.GetType();
FieldInfo[] fiedls = t.GetFields();
for (int i = 0; i < fiedls.Length; i++)
{
string saveName = name + "." + fiedls[i].Name;
switch (fiedls[i].FieldType.Name)
{
case "String":
CryptoPrefs.SetString(saveName, fiedls[i].GetValue(o).ToString());
break;
case "Int32":
case "Int64":
case "Int":
case "uInt":
CryptoPrefs.SetInt(saveName, (int)fiedls[i].GetValue(o));
break;
case "Float":
CryptoPrefs.SetFloat(saveName, (float)fiedls[i].GetValue(o));
break<