Unity3D教程宝典之两步实现超实用的XML存档

前端之家收集整理的这篇文章主要介绍了Unity3D教程宝典之两步实现超实用的XML存档前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
两步实现超实用的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. publicstring key;
  19. //下面是添加需要储存的内容//
  20. publicstring PlayerName;
  21. publicfloat MusicVolume;
  22. publicGameData()
  23. {
  24. PlayerName ="Player";
  25. MusicVolume= 0.6f;
  26. }
  27. }
  28. //管理数据储存的类//
  29. public class GameDataManager:MonoBehavIoUr
  30. {
  31. privatestring dataFileName ="tankyWarData.dat";//存档文件名称,自己定//
  32. private XmlSaver xs = new XmlSaver();
  33. public GameData gameData;
  34. publicvoid Awake()
  35. {
  36. gameData =new GameData();
  37. //设定密钥,根据具体平台设定//
  38. gameData.key= SystemInfo.deviceUniqueIdentifier;
  39. Load();
  40. }
  41. //存档时调用函数//
  42. public void Save()
  43. {
  44. stringgameDataFile = GetDataPath() + "/"+dataFileName;
  45. stringdataString= xs.SerializeObject(gameData,typeof(GameData));
  46. xs.CreateXML(gameDataFile,dataString);
  47. }
  48. //读档时调用函数//
  49. public void Load()
  50. {
  51. stringgameDataFile = 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. privatestatic string GetDataPath()
  75. {
  76. // Your gamehas read+write access to/var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/Documents
  77. //Application.dataPath returnsar/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. // Stripapplication name
  83. path =path.Substring(0,path.LastIndexOf('/'));
  84. return path+ "/Documents";
  85. }
  86. else
  87. // returnApplication.dataPath + "/Resources";
  88. returnApplication.dataPath;
  89. }
  90. }



XmlSaver.cs的内容
  1. //=========================================================================================================
  2. //Note: XML processcing, can not savemultiple-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. publicstring 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. returnConvert.ToBase64String(resultArray,resultArray.Length);
  29. }
  30. //内容解密
  31. publicstring 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. returnUTF8Encoding.UTF8.GetString(resultArray);
  43. }
  44. publicstring SerializeObject(object pObject,System.Type ty)
  45. {
  46. stringXmlizedString = null;
  47. MemoryStreammemoryStream = new MemoryStream();
  48. XmlSerializerxs = new XmlSerializer(ty);
  49. XmlTextWriterxmlTextWriter = 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. publicobject DeserializeObject(string pXmlizedString,System.Typety)
  56. {
  57. XmlSerializerxs = new XmlSerializer(ty);
  58. MemoryStreammemoryStream = newMemoryStream(StringToUTF8ByteArray(pXmlizedString));
  59. XmlTextWriterxmlTextWriter = newXmlTextWriter(memoryStream,Encoding.UTF8);
  60. returnxs.Deserialize(memoryStream);
  61. }
  62. //创建XML文件
  63. publicvoid 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. publicstring 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. publicbool hasFile(String fileName)
  82. {
  83. returnFile.Exists(fileName);
  84. }
  85. publicstring UTF8ByteArrayToString(byte[] characters)
  86. {
  87. UTF8Encodingencoding = new UTF8Encoding();
  88. stringconstructedString =encoding.GetString(characters);
  89. return(constructedString);
  90. }
  91. publicbyte[] StringToUTF8ByteArray(String pXmlString )
  92. {
  93. UTF8Encodingencoding = new UTF8Encoding();
  94. byte[]byteArray = encoding.GetBytes(pXmlString);
  95. return byteArray;
  96. }
  97. }
原文链接:https://www.f2er.com/xml/296288.html

猜你在找的XML相关文章