using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using System; using System.Security.Cryptography; using System.Text; using System.IO; using System.Security.AccessControl; using System.Security.Principal; using System.Xml; using System.Xml.Serialization; public class LinqToXMLAndEncrypt { static string dataKey = SystemInfo.deviceUniqueIdentifier; //设置秘钥,根据平台而定 static string xmlpath = Application.dataPath + @"\mySaveDB"; //存档路径 //加密和解密的key static string securitykey = "12348578902223367877723456789012"; /// <summary> /// 初始化一个XML文件 /// </summary> public static void CreateXMLDocument() { /*定义XML文档结构X,Y,Z用来存储玩家坐标*/ XElement root = new XElement("XMLContent", new XElement("X", new XAttribute("MyVaule", "0")), new XElement("Y", new XAttribute("MyVaule", "0")), new XElement("Z", new XAttribute("MyVaule", "0")), new XElement("Root", "root") ); root.Save(xmlpath); EncrtyptSaveXML(); } private static void EncrtyptSaveXML() { StreamReader sReader = File.OpenText(xmlpath); string xmlData = sReader.ReadToEnd(); sReader.Close(); string xxx = Encrypt(xmlData); StreamWriter writer; writer = File.CreateText(xmlpath); writer.Write(xxx); writer.Close(); } public static XElement DecrtyptLoadXML() { if (hasFile(xmlpath)) { StreamReader sReader = File.OpenText(xmlpath); string xmlData = sReader.ReadToEnd(); sReader.Close(); string xxx = Decrypt(xmlData); StreamWriter writer; writer = File.CreateText(xmlpath); writer.Write(xxx); writer.Close(); XElement root = XElement.Load(xmlpath); return root; } else return null; } public static void SetElementValue(string name, string value) { XElement root = DecrtyptLoadXML(); root.Element(name).SetAttributeValue("MyVaule", value); root.Save(xmlpath); EncrtyptSaveXML(); } /// <summary> /// 在根节点元素之前添加新的元素 /// </summary> /// <param name="name">元素名字</param> /// <param name="value">元素的值</param> public static void AddElement(string name, string value) { XElement root = DecrtyptLoadXML(); root.Element("Root").AddBeforeSelf(new XElement(name, new XAttribute("MyValue", value))); root.Save(xmlpath); EncrtyptSaveXML(); } /// <summary> /// 删除指定的元素 /// </summary> /// <param name="name">要删除的元素名称</param> public static void RemoveElement(string name) { XElement root = DecrtyptLoadXML(); root.Element(name).Remove(); root.Save(xmlpath); EncrtyptSaveXML(); } /// <summary> /// 根据元素名查找元素对应的值 /// </summary> /// <param name="name">元素名</param> /// <returns></returns> public static string GetElementValue(string name) { XElement root = DecrtyptLoadXML(); XAttribute xattr = root.Element(name).Attribute("MyVaule"); string s = xattr.Value; EncrtyptSaveXML(); return s; } /// <summary> /// 内容加密,加密和解密采用相同的key,具体可以自己定义,条件是必须是32位的 /// </summary> /// <param name="toE"></param> /// <returns></returns> private static string Encrypt(string toE) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(securitykey); 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> /// 内容解密,千万记住解密和加密采用相同的key,必须是32位 /// </summary> /// <param name="toD"></param> /// <returns></returns> private static string Decrypt(string toD) { //加密和解密采用相同的key,具体值自己填,但是必须为32位// byte[] keyArray = UTF8Encoding.UTF8.GetBytes(securitykey); 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); } /// <summary> /// 判断XML文档是否存在 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static bool hasFile(string fileName) { return File.Exists(fileName); } /// <summary> /// 读取XML,返回XML的字符串 /// </summary> /// <returns>返回XML的一串字符</returns> public static string LoadXMLForString(bool isDecrypt = true) { if (hasFile(xmlpath)) { StreamReader sReader = File.OpenText(xmlpath); string dataString = sReader.ReadToEnd(); sReader.Close(); if (isDecrypt) { string xxx = Decrypt(dataString); return xxx; } else return dataString; } else { return null; } } }
玩家WSAD移动的脚本,很简单了
using UnityEngine; using System.Collections; public class MovePlayer : MonoBehaviour { public float speed; // Use this for initialization void Start () { speed = 5f; } void Move() { if (Input.GetKey(KeyCode.A)) { transform.Translate( -speed*Time.deltaTime ,0,0 ); } if (Input.GetKey(KeyCode.D)) { transform.Translate(speed * Time.deltaTime, 0, 0); } if (Input.GetKey(KeyCode.W)) { transform.Translate(0, 0, speed * Time.deltaTime); } if (Input.GetKey(KeyCode.S)) { transform.Translate(0, 0, -speed * Time.deltaTime); } } // Update is called once per frame void Update () { Move(); } }
存档和读档的 操作
using UnityEngine; using System.Collections; using System.Linq; using System.Xml.Linq; using System; public class SaveLoadTest : MonoBehaviour { public GameObject player; // Use this for initialization void Start () { //第一次运行初始化 //LinqToXMLAndEncrypt.CreateXMLDocument(); Debug.Log(LinqToXMLAndEncrypt.LoadXMLForString(true)); Debug.Log("Player: Xpos-" + LinqToXMLAndEncrypt.GetElementValue("X")+ "--Ypos-" + LinqToXMLAndEncrypt.GetElementValue("Y")+ "--Zpos-" + LinqToXMLAndEncrypt.GetElementValue("X")); } public void SaveXYZ() { string x = player.transform.position.x.ToString(); string y = player.transform.position.y.ToString(); string z = player.transform.position.z.ToString(); LinqToXMLAndEncrypt.SetElementValue("X", x); LinqToXMLAndEncrypt.SetElementValue("Y", y); LinqToXMLAndEncrypt.SetElementValue("Z", z); } public void LoadXYZ() { float rx = Convert.ToSingle( LinqToXMLAndEncrypt.GetElementValue("X") ); float ry = Convert.ToSingle( LinqToXMLAndEncrypt.GetElementValue("Y") ); float rz = Convert.ToSingle( LinqToXMLAndEncrypt.GetElementValue("Z") ); player.transform.position = new Vector3(rx,ry,rz); } }
Unity简单场景搭建
Player拖放添加MovePlayer.cs脚本 控制移动
新建空object,命名SaveLoadXML 并添加SaveLoadTest.cs脚本,将Player对象赋值给SaveLoadTest中的player
新建两个按钮Button,分别赋予按钮点击事件到SaveXYZ和LoadXYZ方法
运行一次游戏后,注释掉SaveLoadTest中的
//第一次运行初始化
//LinqToXMLAndEncrypt.CreateXMLDocument();
这一行代码
点击Save可以存储玩家坐标
退出
再次进入点击Load可读取玩家坐标
时间: 2024-10-03 08:35:10