【风宇冲】Unity3D教程宝典之两步实现超实用的XML存档

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

原创文章如需转载请注明:转载自风宇冲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. private XmlSaver xs = new XmlSaver();
  33. public GameData gameData;
  34. public void Awake()
  35. {
  36. gameData = new GameData();
  37. //设定密钥,根据具体平台设定//
  38. gameData.key = SystemInfo.deviceUniqueIdentifier;
  39. Load();
  40. }
  41. //存档时调用函数//
  42. public void Save()
  43. {
  44. string gameDataFile = GetDataPath() + "/"+dataFileName;
  45. string dataString= xs.SerializeObject(gameData,typeof(GameData));
  46. xs.CreateXML(gameDataFile,dataString);
  47. }
  48. //读档时调用函数//
  49. public void 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的内容
  1. //=========================================================================================================
  2. //Note: XML processcing, can not save multiple-array!!!
  3. //Date Created: 2012/04/17 by 风宇冲
  4. //Date Modified: 2012/04/19 by 风宇冲
  5. //=========================================================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. using System.Xml;
  9. using System.Xml.Serialization;
  10. using System.IO;
  11. using System.Text;
  12. using System.Security.Cryptography;
  13. using System;
  14. public class XmlSaver
  15. {
  16. //内容加密
  17. public string Encrypt(string toE)
  18. {
  19. //加密和解密采用相同的key,具体自己填,但是必须为32位//
  20. byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12348578902223367877723456789012");
  21. RijndaelManaged rDel = new RijndaelManaged();
  22. rDel.Key = keyArray;
  23. rDel.Mode = CipherMode.ECB;
  24. rDel.Padding = PaddingMode.PKCS7;
  25. ICryptoTransform cTransform = rDel.CreateEncryptor();
  26. byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toE);
  27. byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray,toEncryptArray.Length);
  28. return Convert.ToBase64String(resultArray,resultArray.Length);
  29. }
  30. //内容解密
  31. public string Decrypt(string toD)
  32. {
  33. //加密和解密采用相同的key,具体值自己填,但是必须为32位//
  34. byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12348578902223367877723456789012");
  35. RijndaelManaged rDel = new RijndaelManaged();
  36. rDel.Key = keyArray;
  37. rDel.Mode = CipherMode.ECB;
  38. rDel.Padding = PaddingMode.PKCS7;
  39. ICryptoTransform cTransform = rDel.CreateDecryptor();
  40. byte[] toEncryptArray = Convert.FromBase64String(toD);
  41. byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray,toEncryptArray.Length);
  42. return UTF8Encoding.UTF8.GetString(resultArray);
  43. }
  44. public string SerializeObject(object pObject,System.Type ty)
  45. {
  46. string XmlizedString = null;
  47. MemoryStream memoryStream = new MemoryStream();
  48. XmlSerializer xs = new XmlSerializer(ty);
  49. XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,Encoding.UTF8);
  50. xs.Serialize(xmlTextWriter,pObject);
  51. memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
  52. XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
  53. return XmlizedString;
  54. }
  55. public object DeserializeObject(string pXmlizedString,System.Type ty)
  56. {
  57. XmlSerializer xs = new XmlSerializer(ty);
  58. MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
  59. XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,Encoding.UTF8);
  60. return xs.Deserialize(memoryStream);
  61. }
  62. //创建XML文件
  63. public void CreateXML(string fileName,string thisData)
  64. {
  65. string xxx = Encrypt(thisData);
  66. StreamWriter writer;
  67. writer = File.CreateText(fileName);
  68. writer.Write(xxx);
  69. writer.Close();
  70. }
  71. //读取XML文件
  72. public string LoadXML(string fileName)
  73. {
  74. StreamReader sReader = File.OpenText(fileName);
  75. string dataString = sReader.ReadToEnd();
  76. sReader.Close();
  77. string xxx = Decrypt(dataString);
  78. return xxx;
  79. }
  80. //判断是否存在文件
  81. public bool hasFile(String fileName)
  82. {
  83. return File.Exists(fileName);
  84. }
  85. public string UTF8ByteArrayToString(byte[] characters )
  86. {
  87. UTF8Encoding encoding = new UTF8Encoding();
  88. string constructedString = encoding.GetString(characters);
  89. return (constructedString);
  90. }
  91. public byte[] StringToUTF8ByteArray(String pXmlString )
  92. {
  93. UTF8Encoding encoding = new UTF8Encoding();
  94. byte[] byteArray = encoding.GetBytes(pXmlString);
  95. return byteArray;
  96. }
  97. }

猜你在找的XML相关文章