首先感谢三位大神的博客 :
http://blog.csdn.net/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");
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;
}
}
}
public static T GetValue(string name) where T : new()
{
T newObj = new T();
Type t = newObj.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":
fiedls[i].SetValue(newObj, CryptoPrefs.GetString(saveName));
break;
case "Int32":
case "Int64":
case "Int":
case "uInt":
fiedls[i].SetValue(newObj, CryptoPrefs.GetInt(saveName));
break;
case "Float":
fiedls[i].SetValue(newObj, CryptoPrefs.GetFloat(saveName));
break;
}
}
return newObj;
}
}
三,加密数据类
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
using System.Collections;
public class CryptoPrefs
{
private static string sKEY = "ZTdkNTNmNDE2NTM3MWM0NDFhNTEzNzU1";
private static string sIV = "4rZymEMfa/PpeJ89qY4gyA==";
public static void SetInt(string key, int val)
{
PlayerPrefs.SetString(CryptoPrefs.GetHash(key), CryptoPrefs.Encrypt(val.ToString()));
}
public static int GetInt(string key, int defaultValue = 0)
{
string @string = CryptoPrefs.GetString(key, defaultValue.ToString());
int result = defaultValue;
int.TryParse(@string, out result);
return result;
}
public static void SetFloat(string key, float val)
{
PlayerPrefs.SetString(CryptoPrefs.GetHash(key), CryptoPrefs.Encrypt(val.ToString()));
}
public static float GetFloat(string key, float defaultValue = 0f)
{
string @string = CryptoPrefs.GetString(key, defaultValue.ToString());
float result = defaultValue;
float.TryParse(@string, out result);
return result;
}
public static void SetString(string key, string val)
{
PlayerPrefs.SetString(CryptoPrefs.GetHash(key), CryptoPrefs.Encrypt(val));
}
public static string GetString(string key, string defaultValue = "")
{
string text = defaultValue;
string @string = PlayerPrefs.GetString(CryptoPrefs.GetHash(key), defaultValue.ToString());
if (!text.Equals(@string))
{
text = CryptoPrefs.Decrypt(@string);
}
return text;
}
public static bool HasKey(string key)
{
string hash = CryptoPrefs.GetHash(key);
return PlayerPrefs.HasKey(hash);
}
public static void DeleteKey(string key)
{
string hash = CryptoPrefs.GetHash(key);
PlayerPrefs.DeleteKey(hash);
}
public static void DeleteAll()
{
PlayerPrefs.DeleteAll();
}
public static void Save()
{
PlayerPrefs.Save();
}
private static string Decrypt(string encString)
{
RijndaelManaged rijndaelManaged = new RijndaelManaged
{
Padding = PaddingMode.Zeros,
Mode = CipherMode.CBC,
KeySize = 128,
BlockSize = 128
};
byte[] bytes = Encoding.UTF8.GetBytes(CryptoPrefs.sKEY);
byte[] rgbIV = Convert.FromBase64String(CryptoPrefs.sIV);
ICryptoTransform transform = rijndaelManaged.CreateDecryptor(bytes, rgbIV);
byte[] array = Convert.FromBase64String(encString);
byte[] array2 = new byte[array.Length];
MemoryStream stream = new MemoryStream(array);
CryptoStream cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Read);
cryptoStream.Read(array2, 0, array2.Length);
Debug.Log("Encoding.UTF8.GetString(array2).TrimEnd(new char[1] " + Encoding.UTF8.GetString(array2).TrimEnd(new char[1]));
return Encoding.UTF8.GetString(array2).TrimEnd(new char[1]);
}
private static string Encrypt(string rawString)
{
RijndaelManaged rijndaelManaged = new RijndaelManaged
{
Padding = PaddingMode.Zeros,
Mode = CipherMode.CBC,
KeySize = 128,
BlockSize = 128
};
byte[] bytes = Encoding.UTF8.GetBytes(CryptoPrefs.sKEY);
byte[] rgbIV = Convert.FromBase64String(CryptoPrefs.sIV);
ICryptoTransform transform = rijndaelManaged.CreateEncryptor(bytes, rgbIV);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
byte[] bytes2 = Encoding.UTF8.GetBytes(rawString);
cryptoStream.Write(bytes2, 0, bytes2.Length);
cryptoStream.FlushFinalBlock();
byte[] inArray = memoryStream.ToArray();
return Convert.ToBase64String(inArray);
}
private static string GetHash(string key)
{
MD5 mD = new MD5CryptoServiceProvider();
byte[] array = mD.ComputeHash(Encoding.UTF8.GetBytes(key));
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < array.Length; i++)
{
stringBuilder.Append(array[i].ToString("x2"));
}
return stringBuilder.ToString();
}
// //默认密钥向量
// private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
// public static string keyss = "1234567z";
// ///
// /// DES加密字符串
// ///
// /// 待加密的字符串
// /// 加密密钥,要求为8位
// /// 加密成功返回加密后的字符串,失败返回源串
// public static string EncryptDES(string encryptString, string encryptKey)
// {
// try
// {
// byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
// byte[] rgbIV = Keys;
// byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
// DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
// MemoryStream mStream = new MemoryStream();
// CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
// cStream.Write(inputByteArray, 0, inputByteArray.Length);
// cStream.FlushFinalBlock();
// return Convert.ToBase64String(mStream.ToArray());
// }
// catch
// {
// return encryptString;
// }
// }///
// /// DES解密字符串
// ///
// /// 待解密的字符串
// /// 解密密钥,要求为8位,和加密密钥相同
// /// 解密成功返回解密后的字符串,失败返源串
// public static string DecryptDES(string decryptString, string decryptKey)
// {
// try
// {
// byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
// byte[] rgbIV = Keys;
// byte[] inputByteArray = Convert.FromBase64String(decryptString);
// DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
// MemoryStream mStream = new MemoryStream();
// CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
// cStream.Write(inputByteArray, 0, inputByteArray.Length);
// cStream.FlushFinalBlock();
// return Encoding.UTF8.GetString(mStream.ToArray());
// }
// catch
// {
// return decryptString;
// }
// }
}
playerPrefs 本地存储路径可以参考 官方文档
各平台保存路径
On Mac OS X PlayerPrefs are stored in ~/Library/Preferences folder, in a file named unity.[company name].[product name].plist, where company and product names are the names set up in Project Settings. The same .plist file is used for both Projects run in the Editor and standalone players.
On Windows, PlayerPrefs are stored in the registry under HKCU\Software[company name][product name] key, where company and product names are the names set up in Project Settings.
On Linux, PlayerPrefs can be found in ~/.config/Unity3D/[CompanyName]/[ProductName] again using the company and product names specified in the Project Settings.
On Windows Store Apps, Player Prefs can be found in %userprofile%\AppData\Local\Packages[ProductPackageId]>\LocalState\playerprefs.dat
On Windows Phone 8, Player Prefs can be found in application’s local folder, See Also: Windows.Directory.localFolder
WebPlayer
On Web players, PlayerPrefs are stored in binary files in the following locations:
Mac OS X: ~/Library/Preferences/Unity/WebPlayerPrefs
Windows: %APPDATA%\Unity\WebPlayerPrefs
以上是此次总结。