前言
相对于单机游戏或者弱联网游戏会有一个本地数据缓存的问题,联网游戏也会有一些设置需要缓存在本地.也算是记录一下Application.persistentDataPath的方法. Application.persistentDataPath好像是移动端位移可写可读的路劲,对于热更有独到的地方 本文记录一个移动端写入,读取数据对数据进行加密的操作
一、写入数据
- 使用到LitJson,和ICSharpCode.SharpZipLib.GZip工具
- 先定义一个写入路径,创建一个路径脚本,定义路径
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathRule
{
public static string ResPath
{
get { return AppPersistentDataPath + "SJ_RES/"; }
}
public static readonly string AppPersistentDataPath =
#if UNITY_EDITOR
System.Environment.CurrentDirectory + "/";
#elif UNITY_IPHONE
Application.persistentDataPath + "/";
#elif UNITY_ANDROID
Application.persistentDataPath + "/";
#else
System.Environment.CurrentDirectory + "/";
#endif
}
2.创建一个SettingData脚本,这里面有一个加载,保存数据的方法,这两个也可以封装成一个接口给外部调用,加密解密的代码注销掉了,加密解密也只是把数据包装成一个人看不懂的数据,用不到加密的看上面这两个脚本就行了,只不过目前是明文保存
using LitJson;
using System;
using System.Globalization;
using System.IO;
using System.Text;
using UnityEngine;
public class SettingData
{
public static SettingInfo setting { private set; get; }
public static void InitSystemSet()
{
setting = new SettingInfo();
LoadUserSetting();
Debug.Log(setting.name);
}
private static void LoadUserSetting()
{
try
{
string settingString = System.IO.File.ReadAllText(PathRule.ResPath + "setting.txt");
setting = JsonMapper.ToObject<SettingInfo>(settingString);
if (setting == null)
setting = new SettingInfo();
}
catch (Exception e)
{
setting = new SettingInfo();
}
}
public static void SaveUserSetting()
{
if (!Directory.Exists(PathRule.ResPath))
{
Directory.CreateDirectory(PathRule.ResPath);
}
System.IO.File.WriteAllText(PathRule.ResPath + "setting.txt" , JsonMapper.ToJson(setting), Encoding.UTF8);
}
}
public class SettingInfo
{
public string name;
}
3.使用demo,创建一个Demo脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class demo : MonoBehaviour
{
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
SettingData.InitSystemSet();
SettingData.setting.name = "周亚伟";
SettingData.SaveUserSetting();
}
}
}
4.这样就可以在项目下看到这个新建文件
二、加密.解密
1.创建CodeUtil脚本 主要是使用到了Encode(),Decode()两个方法 里面还用到了其他脚本的一些引用,有些判断被简化了,看个人需求a
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System;
using System.Security.Cryptography;
using ICSharpCode.SharpZipLib.GZip;
using LitJson;
public class CodeUtil
{
public static T ToObject<T>(string jsStr)
{
return JsonMapper.ToObject<T>(jsStr);
}
public static string ToJson(object o)
{
return JsonMapper.ToJson(o);
}
static string KEY_64 = "12345678";
static string IV_64 = "87654321";
public static string Encode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms , cryptoProvider.CreateEncryptor(byKey , byIV) , CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer() , 0 , (int)ms.Length);
}
public static string Decode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms , cryptoProvider.CreateDecryptor(byKey , byIV) , CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}
}
加密就是下面这种,不是明文了,一般人也看不懂,但二班的也能看出来
总结
csdn资源下载地址 没了
|