标签:
学习笔记适合新手,如有错误请指正。?号处也请各位指点下,谢谢。
定义数据结构
创建CharData类,里边包含了角色名字等信息
public class CharData { public bool created = false;//false:未创建角色 true:已创建角色 public string name;//名字 public int occupation;//职业: 0=战士 1=法师 }
数据管理类
加密的过程是通过JsonFx将CharData对象转化成JSON文本数据,经过加密后,写入文件,解密的过程则是加密过程的逆向过程,即从文件里读取加密后的数据并解密,然后用JsonFx将数据转化成CharData对象,如下:
using UnityEngine; using System.Collections; using System; using JsonFx.Json; using System.IO; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; /// <summary> /// 数据管理器 /// </summary> public class DataManager { #region Config //文档相对路径 static string fileName = "Chapter8/8.3/data.dat"; //Rijndael加密算法的256位秘钥 static string key = "12348578902223367877723456789012"; #endregion private static CharData CharData_instance; public static CharData CharData_Instance { get { if (CharData_instance == null) { if (ExistFile () == false) CharData_instance = new CharData (); else Load (); } return CharData_instance; } set { CharData_instance = value; Save (); } } /// <summary> /// 是否存在该文件 /// </summary> public static bool ExistFile () { return File.Exists (GetDataPath () + "/" + fileName); } /// <summary> /// 删除该文件 /// </summary> public static void DeleteDataFile () { if (ExistFile ()) File.Delete (GetDataPath () + "/" + fileName); } /// <summary> /// 保存该文件 /// </summary> public static void Save () { string text = JsonFx.Json.JsonWriter.Serialize (CharData_Instance); // text = Encrypt(text); File.WriteAllText (GetDataPath () + "/" + fileName, text); } /// <summary> /// 读取该文件 /// </summary> public static void Load () { if (File.Exists (GetDataPath () + "/" + fileName) == false) return; string orginText = File.ReadAllText (GetDataPath () + "/" + fileName); // orginText = Decrypt(orginText); CharData_Instance = JsonFx.Json.JsonReader.Deserialize<CharData> (orginText); } /// <summary> /// 得到设备路径 /// </summary> private static string GetDataPath () { if (Application.platform == RuntimePlatform.IPhonePlayer) { //iphone路径 string path = Application.dataPath.Substring (0, Application.dataPath.Length - 5); path = path.Substring (0, path.LastIndexOf (‘/‘)); return path + "/Documents"; } else if (Application.platform == RuntimePlatform.Android) { //安卓路径 return Application.persistentDataPath + "/"; } else { //其他路径 return Application.dataPath; } } /// <summary> /// 字符串加密 /// </summary> private static string Encrypt (string toE) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes (key); RijndaelManaged rDel = new RijndaelManaged (); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor (); byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes (toE); byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String (resultArray, 0, resultArray.Length); } /// <summary> /// 字符串解密 /// </summary> private static string Decrypt (string toD) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes (key); RijndaelManaged rDel = new RijndaelManaged (); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor (); byte[] toEncryptArray = Convert.FromBase64String (toD); byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length); return UTF8Encoding.UTF8.GetString (resultArray); } }
创建界面
创建角色页面page_create和剧情页面page_srart
脚本里首先获取了名字输入框等UI组件,然后通过CharData对象里的created参数判断是否创建了角色,如果没有显示创建角色页面,反之则显示剧情页面
using UnityEngine; using System.Collections; using UnityEngine.UI; public class C_10_3 : MonoBehaviour { public GameObject page_create;//创建角色页面 public InputField inputField_name;//名字输入 public Toggle toggle_warrior;//选择按钮:战士 public GameObject page_start;//剧情页面 public Text start_text;//剧情文字 string[] OccupationCn = {"战士","魔法师"};//职业中文名 void Start() { if(!DataManager.CharData_Instance.created) { //如果尚未创建角色,则显示创建角色页面 page_create.SetActive(true); page_start.SetActive(false); } else { //已经创建角色,则显示剧情页面 StartGame(); } } /// <summary> /// 点击创建按钮 /// </summary> public void On_Create() { //如果玩家没有填写名字,可以提醒玩家 if(string.IsNullOrEmpty(inputField_name.text)) { Debug.Log("尚未填写名字"); return; } //创建角色 DataManager.CharData_Instance.created = true;//设置已创建标记 DataManager.CharData_Instance.name = inputField_name.text;//保存名字 //保存职业 if(toggle_warrior.isOn) DataManager.CharData_Instance.occupation = 0; else DataManager.CharData_Instance.occupation = 1; //保存数据 DataManager.Save(); //显示剧情页面 StartGame(); } /// <summary> /// 显示剧情页面 /// </summary> void StartGame() { page_create.SetActive(false);//隐藏创建角色页面 page_start.SetActive(true);//显示剧情页面 //获取职业中文名 string occCn = OccupationCn[DataManager.CharData_Instance.occupation]; //剧情文字 start_text.text = string.Format( "\t在这片美丽富饶的土地上,有\n一位名叫{0}的青年,职业是\n{1}...",DataManager.CharData_Instance.name, occCn); } }
没事试试看不加密的存档文件和加密后的存档文件内容有什么不同,把DataManager.cs的Save()函数里的Text=Encrypt(Text);和Load()函数里的orginText=Decrypt(orginText);这两句代码用//注释掉是,输入名字并选择职业,点击创建按钮,退出运行,再把data.dat改后缀名为.text,然后打开内容为标准的JSON格式字符串,如下所示:
{"created":true,"name":"Jack","occupation":1}
把data.txt文件删除,并把两句代码去掉注释,重新创建相同的角色,发现data.txt打开后内容为加密后的字符串乱码
标签:
原文地址:http://www.cnblogs.com/kubll/p/4934922.html