M层其实就是指数据层,是比较纯粹的,因此先讲,我认为将数据的读取、保存、服务器数据的交互等数据相关操作都放在这里管理最佳。
M层的管理很简单,基本就是一些数据的定义、读写、委托回调:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 数据控制中心,只负责数据的读取、保存,所有数据放在此处,通过单例访问
/// 即为MVC的M层
/// </summary>
public class Data : Singleton<Data>
{
private const string m_BloodVolumeName = "BloodVolume";
private int m_BloodVolume = 100;
/// <summary>
/// 血量
/// </summary>
public int BloodVolume
{
get => m_BloodVolume;
set
{
m_BloodVolume = Mathf.Clamp(value, 0, 100);
//每次修改时保存血量,仅为示例,也可放在其他地方调用
SaveBloodVolume();
//血量变动时回调
if (OnBloodVolumeChanged != null)
OnBloodVolumeChanged();
}
}
public Action OnBloodVolumeChanged;
/// <summary>
/// 读取数据
/// </summary>
public void ReadBloodVolume()
{
string bloodVolume = "" + BloodVolume;
if (PlayerPrefs.HasKey(m_BloodVolumeName))
bloodVolume = EncryptHelp.Decrypt(PlayerPrefs.GetString(m_BloodVolumeName));
BloodVolume = System.Convert.ToInt32(bloodVolume);
}
/// <summary>
/// 保存数据
/// </summary>
public void SaveBloodVolume()
{
string bloodVolume = "" + BloodVolume;
PlayerPrefs.SetString(m_BloodVolumeName, EncryptHelp.Encrypt(bloodVolume));
}
}
这里用到了一个简单的加密类,不用也没关系~:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
public static class EncryptHelp
{
/// <summary>
/// 加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Encrypt(string str)
{
return EncryptDES(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(str)));
}
/// <summary>
/// 解密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Decrypt(string str)
{
return System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(DecryptDES(str)));
}
#region 字符串加密解密
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="key">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
private static string EncryptDES(string encryptString, string key = "13717421")
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(key.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();
cStream.Close();
return Convert.ToBase64String(mStream.ToArray());
}
catch
{
//Debug.LogError("StringEncrypt/EncryptDES()/ Encrypt error!");
return encryptString;
}
}
/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="key">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
private static string DecryptDES(string decryptString, string key = "13717421")
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(key);
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();
cStream.Close();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
//Debug.LogError("StringEncrypt/DecryptDES()/ Decrypt error!");
return decryptString;
}
}
#endregion
}