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

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

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

两步实现超实用的XML存档

本套存档的优点:易使用,跨平台,防作弊(内容加密 + 防拷贝)

脚本下载地址

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

iOS,Android,PC,MAC都使用过的。密钥的设定根据平台而定。

GameDataManager.cs的内容

复制代码

  1. //=========================================================================================================
  2. //Note:DataManaging.
  3. //DateCreated:2012/04/17by风宇冲
  4. //DateModified:2012/12/14by风宇冲
  5. //=========================================================================================================usingUnityEngine;
  6. usingSystem.Collections;
  7. usingSystem.IO;
  8. usingSystem.Collections.Generic;
  9. usingSystem;
  10. usingSystem.Text;
  11. usingSystem.Xml;
  12. usingSystem.Security.Cryptography;
  13. //GameData,储存数据的类,把需要储存的数据定义在GameData之内就行//publicclassGameData
  14. {
  15. //密钥,用于防止拷贝存档//
  16. publicstringkey;
  17.  
  18. //下面是添加需要储存的内容//
  19. publicstringPlayerName;
  20. publicfloatMusicVolume;
  21. publicGameData()
  22. {
  23. PlayerName="Player";
  24. MusicVolume=0.6f;
  25. }
  26. }
  27. //管理数据储存的类//publicclassGameDataManager:MonoBehavIoUr
  28. {
  29. privatestringdataFileName="tankyWarData.dat";//存档文件名称,自己定//
  30. privateXmlSaverxs=newXmlSaver();
  31.  
  32. publicGameDatagameData;
  33.  
  34. publicvoidAwake()
  35. {
  36. gameData=newGameData();
  37.  
  38. //设定密钥,根据具体平台设定//
  39. gameData.key=SystemInfo.deviceUniqueIdentifier;
  40. Load();
  41. }
  42.  
  43. //存档时调用函数//
  44. publicvoidSave()
  45. {
  46. stringgameDataFile=GetDataPath()+"/"+dataFileName;
  47. stringdataString=xs.SerializeObject(gameData,typeof(GameData));
  48. xs.CreateXML(gameDataFile,dataString);
  49. }
  50.  
  51. //读档时调用函数//
  52. publicvoidLoad()
  53. {
  54. stringgameDataFile=GetDataPath()+"/"+dataFileName;
  55. if(xs.hasFile(gameDataFile))
  56. {
  57. stringdataString=xs.LoadXML(gameDataFile);
  58. GameDatagameDataFromXML=xs.DeserializeObject(dataString,typeof(GameData))asGameData;
  59.  
  60. //是合法存档//
  61. if(gameDataFromXML.key==gameData.key)
  62. {
  63. gameData=gameDataFromXML;
  64. }
  65. //是非法拷贝存档//
  66. else
  67. {
  68. //留空:游戏启动后数据清零,存档后作弊档被自动覆盖//}
  69. }
  70. else
  71. {
  72. if(gameData!=null)
  73. Save();
  74. }
  75. }
  76.  
  77. //获取路径//
  78. privatestaticstringGetDataPath()
  79. {
  80. //Yourgamehasread+writeaccessto/var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/Documents
  81. //Application.dataPathreturnsar/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/myappname.app/Data
  82. //Strip"/Data"frompath
  83. if(Application.platform==RuntimePlatform.IPhonePlayer)
  84. {
  85. stringpath=Application.dataPath.Substring(0,Application.dataPath.Length-5);
  86. //Stripapplicationname
  87. path=path.Substring(0,path.LastIndexOf('/'));
  88. returnpath+"/Documents";
  89. }
  90. else
  91. //returnApplication.dataPath+"/Resources";
  92. returnApplication.dataPath;
  93. }
  94. }

复制代码

XmlSaver.cs

复制代码

  1. //=========================================================================================================
  2. //Note:XMLprocesscing,cannotsavemultiple-array!!!
  3. //DateCreated:2012/04/17by风宇冲
  4. //DateModified:2012/04/19by风宇冲
  5. //=========================================================================================================usingUnityEngine;
  6. usingSystem.Collections;
  7. usingSystem.Xml;
  8. usingSystem.Xml.Serialization;
  9. usingSystem.IO;
  10. usingSystem.Text;
  11. usingSystem.Security.Cryptography;
  12. usingSystem;
  13. publicclassXmlSaver
  14. {
  15. //内容加密
  16. publicstringEncrypt(stringtoE)
  17. {
  18. //加密和解密采用相同的key,具体自己填,但是必须为32位//
  19. byte[]keyArray=UTF8Encoding.UTF8.GetBytes("12348578902223367877723456789012");
  20. RijndaelManagedrDel=newRijndaelManaged();
  21. rDel.Key=keyArray;
  22. rDel.Mode=CipherMode.ECB;
  23. rDel.Padding=PaddingMode.PKCS7;
  24. ICryptoTransformcTransform=rDel.CreateEncryptor();
  25.  
  26. byte[]toEncryptArray=UTF8Encoding.UTF8.GetBytes(toE);
  27. byte[]resultArray=cTransform.TransformFinalBlock(toEncryptArray,toEncryptArray.Length);
  28.  
  29. returnConvert.ToBase64String(resultArray,resultArray.Length);
  30. }
  31.  
  32. //内容解密
  33. publicstringDecrypt(stringtoD)
  34. {
  35. //加密和解密采用相同的key,具体值自己填,但是必须为32位//
  36. byte[]keyArray=UTF8Encoding.UTF8.GetBytes("12348578902223367877723456789012");
  37.  
  38. RijndaelManagedrDel=newRijndaelManaged();
  39. rDel.Key=keyArray;
  40. rDel.Mode=CipherMode.ECB;
  41. rDel.Padding=PaddingMode.PKCS7;
  42. ICryptoTransformcTransform=rDel.CreateDecryptor();
  43.  
  44. byte[]toEncryptArray=Convert.FromBase64String(toD);
  45. byte[]resultArray=cTransform.TransformFinalBlock(toEncryptArray,toEncryptArray.Length);
  46.  
  47. returnUTF8Encoding.UTF8.GetString(resultArray);
  48. }
  49.  
  50. publicstringSerializeObject(objectpObject,System.Typety)
  51. {
  52. stringXmlizedString=null;
  53. MemoryStreammemoryStream=newMemoryStream();
  54. XmlSerializerxs=newXmlSerializer(ty);
  55. XmlTextWriterxmlTextWriter=newXmlTextWriter(memoryStream,Encoding.UTF8);
  56. xs.Serialize(xmlTextWriter,pObject);
  57. memoryStream=(MemoryStream)xmlTextWriter.BaseStream;
  58. XmlizedString=UTF8ByteArrayToString(memoryStream.ToArray());
  59. returnXmlizedString;
  60. }
  61.  
  62. publicobjectDeserializeObject(stringpXmlizedString,System.Typety)
  63. {
  64. XmlSerializerxs=newXmlSerializer(ty);
  65. MemoryStreammemoryStream=newMemoryStream(StringToUTF8ByteArray(pXmlizedString));
  66. XmlTextWriterxmlTextWriter=newXmlTextWriter(memoryStream,Encoding.UTF8);
  67. returnxs.Deserialize(memoryStream);
  68. }
  69.  
  70. //创建XML文件
  71. publicvoidCreateXML(stringfileName,stringthisData)
  72. {
  73. stringxxx=Encrypt(thisData);
  74. StreamWriterwriter;
  75. writer=File.CreateText(fileName);
  76. writer.Write(xxx);
  77. writer.Close();
  78. }
  79.  
  80. //读取XML文件
  81. publicstringLoadXML(stringfileName)
  82. {
  83. StreamReadersReader=File.OpenText(fileName);
  84. stringdataString=sReader.ReadToEnd();
  85. sReader.Close();
  86. stringxxx=Decrypt(dataString);
  87. returnxxx;
  88. }
  89.  
  90. //判断是否存在文件
  91. publicboolhasFile(StringfileName)
  92. {
  93. returnFile.Exists(fileName);
  94. }
  95. publicstringUTF8ByteArrayToString(byte[]characters)
  96. {
  97. UTF8Encodingencoding=newUTF8Encoding();
  98. stringconstructedString=encoding.GetString(characters);
  99. return(constructedString);
  100. }
  101.  
  102. publicbyte[]StringToUTF8ByteArray(StringpXmlString)
  103. {
  104. UTF8Encodingencoding=newUTF8Encoding();
  105. byte[]byteArray=encoding.GetBytes(pXmlString);
  106. returnbyteArray;
  107. }
  108. }

猜你在找的XML相关文章