IOHelper
using UnityEngine;
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
public static class IOHelper
{
public static bool IsFileExists(string fileName)
{
return File.Exists(fileName);
}
public static bool IsDirectoryExists(string fileName)
{
return Directory.Exists(fileName);
}
public static void CreateFile(string fileName, string content)
{
StreamWriter streamWriter = File.CreateText(fileName);
streamWriter.Write(content);
streamWriter.Close();
}
public static void CreateDirectory(string fileName)
{
if (IsDirectoryExists(fileName))
return;
Directory.CreateDirectory(fileName);
}
public static void SetData(string fileName, object pObject)
{
string toSave = SerializeObject(pObject);
string Key = SystemInfo.deviceUniqueIdentifier.Substring(0, 32);
toSave = RijndaelEncrypt(toSave, Key);
StreamWriter streamWriter = File.CreateText(fileName);
streamWriter.Write(toSave);
streamWriter.Close();
}
public static object GetData(string fileName, Type pType)
{
StreamReader streamReader = File.OpenText(fileName);
string data = streamReader.ReadToEnd();
string Key = SystemInfo.deviceUniqueIdentifier.Substring(0, 32);
data = RijndaelDecrypt(data, Key);
streamReader.Close();
return DeserializeObject(data, pType);
}
private static string RijndaelEncrypt(string pString, string pKey)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(pKey);
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(pString);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
private static String RijndaelDecrypt(string pString, string pKey)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(pKey);
byte[] toEncryptArray = Convert.FromBase64String(pString);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
private static string SerializeObject(object pObject)
{
string serializedString = string.Empty;
serializedString = JsonUtility.ToJson(pObject);
return serializedString;
}
private static object DeserializeObject(string pString, Type pType)
{
object deserializedObject = null;
deserializedObject = JsonUtility.FromJson(pString, pType);
return deserializedObject;
}
}
Test
public class Test: MonoBehaviour {
public static GameManager instance;
public SettingsData SettingsData = new SettingsData();
void Awake()
{
instance = this;
string dirpath = Application.persistentDataPath + "/Save";
string filename = Application.persistentDataPath + "/Save/xx.sav";
Debug.Log("存档路径:"+Application.persistentDataPath + "/Save");
IOHelper.CreateDirectory(dirpath);
if (IOHelper.IsFileExists(dirpath + "/GameData.sav"))
{
SettingsData = (SettingsData)IOHelper.GetData(filename, typeof(SettingsData));
}
else
{
IOHelper.SetData(filename, SettingsData);
}
}
}
[Serializable]
public class SettingsData
{
public bool Sound = false;
public bool Music = false;
public languages language = languages.Chinese;
}