【风宇冲】Unity3D教程宝典之两步实现超实用的XML存档 内容加密 + 防拷贝

前端之家收集整理的这篇文章主要介绍了【风宇冲】Unity3D教程宝典之两步实现超实用的XML存档 内容加密 + 防拷贝前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://blog.sina.com.cn/s/blog_471132920101d3kh.html


原创文章如需转载请注明:转载自风宇冲Unity3D教程学院


两步实现超实用的XML存档

本套存档的优点:易使用,跨平台,防作弊( 内容加密 + 防拷贝)
脚本下载地址
使用方法非常简单:
把GameDataManager和XmlSaver两个脚本添加至工程
(1)新建一个GameObject,起名GameDataManager并将GameDataManager脚本拖到上面。
(2)在GameDataManager里的GameData类中添加需要储存的数据
OK,跨平台防破解防拷贝的存档就搞定了!之后 每次存档 调用GameDataManager的Save函数 读档 调用GameDataManager的Load函数。每次启动后GameDataManager会自动调用Load读档。如果玩家拿外来存档来覆盖本地存档,则游戏启动后数据清零,任何一次存档后作弊档被自动覆盖。注意:请勿放入二维以上数组,一般一维数据,枚举,自定义类 等等数据类型可放心添加

PS:风宇冲自己的U3d单机游戏和公司的单机游戏都用的这个XML存档,iOS,Android,PC,MAC都使用过的。放心使用吧。密钥的设定根据平台而定。
附:脚本代码
GameDataManager.cs的内容
  1. //=========================================================================================================
  2. //Note: Data Managing.
  3. //Date Created: 2012/04/17 by 风宇冲
  4. //Date Modified: 2012/12/14 by 风宇冲
  5. //=========================================================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using System;
  11. using System.Text;
  12. using System.Xml;
  13. using System.Security.Cryptography;
  14. //GameData,储存数据的类,把需要储存的数据定义在GameData之内就行//
  15. public class GameData
  16. {
  17. //密钥,用于防止拷贝存档//
  18. public string key;
  19. //下面是添加需要储存的内容//
  20. public string PlayerName;
  21. public float MusicVolume;
  22. public GameData()
  23. {
  24. PlayerName = "Player";
  25. MusicVolume = 0.6f;
  26. }
  27. }
  28. //管理数据储存的类//
  29. public class GameDataManager:MonoBehavIoUr
  30. {
  31. private string dataFileName ="tankyWarData.dat";//存档文件名称,自己定//
  32. privateXmlSaver xs = new XmlSaver();
  33. publicGameData gameData;
  34. public void Awake()
  35. {
  36. gameData = new GameData();
  37. //设定密钥,根据具体平台设定//
  38. gameData.key = SystemInfo.deviceUniqueIdentifier;
  39. Load();
  40. }
  41. //存档时调用函数//
  42. publicvoid Save()
  43. {
  44. string gameDataFile = GetDataPath() + "/"+dataFileName;
  45. string dataString= xs.SerializeObject(gameData,typeof(GameData));
  46. xs.CreateXML(gameDataFile,dataString);
  47. }
  48. //读档时调用函数//
  49. publicvoid Load()
  50. {
  51. string gameDataFile = GetDataPath() + "/"+dataFileName;
  52. if(xs.hasFile(gameDataFile))
  53. {
  54. string dataString = xs.LoadXML(gameDataFile);
  55. GameData gameDataFromXML = xs.DeserializeObject(dataString,typeof(GameData)) as GameData;
  56. //是合法存档//
  57. if(gameDataFromXML.key == gameData.key)
  58. {
  59. gameData = gameDataFromXML;
  60. }
  61. //是非法拷贝存档//
  62. else
  63. {
  64. //留空:游戏启动后数据清零,存档后作弊档被自动覆盖//
  65. }
  66. }
  67. else
  68. {
  69. if(gameData != null)
  70. Save();
  71. }
  72. }
  73. //获取路径//
  74. private static string GetDataPath()
  75. {
  76. // Your game has read+write access to /var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/Documents
  77. // Application.dataPath returns ar/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/myappname.app/Data
  78. // Strip "/Data" from path
  79. if(Application.platform == RuntimePlatform.IPhonePlayer)
  80. {
  81. string path = Application.dataPath.Substring (0,Application.dataPath.Length - 5);
  82. // Strip application name
  83. path = path.Substring(0,path.LastIndexOf('/'));
  84. return path + "/Documents";
  85. }
  86. else
  87. //return Application.dataPath + "/Resources";
  88. return Application.dataPath;
  89. }
  90. }

XmlSaver.cs的内容
  • //=========================================================================================================
  • //Note: XML processcing,can not save multiple-array!!!
  • //Date Created: 2012/04/17 by 风宇冲
  • //Date Modified: 2012/04/19 by 风宇冲
  • //=========================================================================================================
  • using UnityEngine;
  • using System.Collections;
  • using System.Xml;
  • using System.Xml.Serialization;
  • using System.IO;
  • using System.Text;
  • using System.Security.Cryptography;
  • using System;
  • public class XmlSaver
  • {
  • //内容加密
  • public string Encrypt(string toE)
  • {
  • //加密和解密采用相同的key,具体自己填,但是必须为32位//
  • byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12348578902223367877723456789012");
  • 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,toEncryptArray.Length);
  • return Convert.ToBase64String(resultArray,resultArray.Length);
  • }
  • //内容解密
  • public string Decrypt(string toD)
  • {
  • //加密和解密采用相同的key,具体值自己填,但是必须为32位//
  • byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12348578902223367877723456789012");
  • 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,toEncryptArray.Length);
  • return UTF8Encoding.UTF8.GetString(resultArray);
  • }
  • public string SerializeObject(object pObject,System.Type ty)
  • {
  • string XmlizedString= null;
  • MemoryStream memoryStream= new MemoryStream();
  • XmlSerializer xs= new XmlSerializer(ty);
  • XmlTextWriter xmlTextWriter= new XmlTextWriter(memoryStream,Encoding.UTF8);
  • xs.Serialize(xmlTextWriter,pObject);
  • memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
  • XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
  • return XmlizedString;
  • }
  • public object DeserializeObject(string pXmlizedString,System.Type ty)
  • {
  • XmlSerializer xs= new XmlSerializer(ty);
  • MemoryStream memoryStream= new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
  • XmlTextWriter xmlTextWriter= new XmlTextWriter(memoryStream,Encoding.UTF8);
  • return xs.Deserialize(memoryStream);
  • }
  • //创建XML文件
  • public void CreateXML(string fileName,string thisData)
  • {
  • string xxx = Encrypt(thisData);
  • StreamWriter writer;
  • writer = File.CreateText(fileName);
  • writer.Write(xxx);
  • writer.Close();
  • }
  • //读取XML文件
  • public string LoadXML(string fileName)
  • {
  • StreamReader sReader = File.OpenText(fileName);
  • string dataString = sReader.ReadToEnd();
  • sReader.Close();
  • string xxx = Decrypt(dataString);
  • return xxx;
  • }
  • //判断是否存在文件
  • public bool hasFile(String fileName)
  • {
  • return File.Exists(fileName);
  • }
  • public string UTF8ByteArrayToString(byte[] characters)
  • {
  • UTF8Encoding encoding= new UTF8Encoding();
  • string constructedString= encoding.GetString(characters);
  • return (constructedString);
  • }
  • public byte[] StringToUTF8ByteArray(String pXmlString )
  • {
  • UTF8Encoding encoding= new UTF8Encoding();
  • byte[] byteArray= encoding.GetBytes(pXmlString);
  • return byteArray;
  • }
  • }
  • 猜你在找的XML相关文章