这个脚本插件就两个脚本类,一个是XmlManager
,它主要是用来序列化和反序列化目标类的,还有加密和解密的功能也写在这里面的。另一个脚本是XMLDataFileEncrypt
,我们需要生成xml的类就必须继承它。下面直接上脚本
using System;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Security.Cryptography;
public class XmlManager
{
private static XmlManager instance = null;
public static XmlManager Instance
{
get
{
if (instance == null)
{
instance = new XmlManager();
}
return instance;
}
}
private bool isEncrypt = false;
public bool IsEncrypt
{
get { return isEncrypt; }
set { isEncrypt = value; }
}
#region GenerateXML
public string serializeObject(object pObject, System.Type ty)
{
MemoryStream mStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(ty);
XmlTextWriter xmlTextWriter = new XmlTextWriter(mStream, Encoding.UTF8);
xmlTextWriter.Formatting = Formatting.Indented;
xs.Serialize(xmlTextWriter, pObject);
mStream = (MemoryStream)xmlTextWriter.BaseStream;
return UTF8ByteArrayToString(mStream.ToArray());
}
public void createXML(string fileName, string dataString)
{
StreamWriter writer;
writer = File.CreateText(fileName);
if(isEncrypt)
writer.Write(encrypt(dataString));
else
writer.Write(dataString);
writer.Close();
}
public string UTF8ByteArrayToString(byte[] bytes)
{
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetString(bytes);
}
#endregion
#region LoadXML
public object deserializeObject(string serializedString, System.Type ty)
{
XmlSerializer xs = new XmlSerializer(ty);
MemoryStream mStream = new MemoryStream(stringToUTF8ByteArray(serializedString));
return xs.Deserialize(mStream);
}
public string loadXML(string fileName)
{
StreamReader reader = File.OpenText(fileName);
string dataString = reader.ReadToEnd();
reader.Close();
if (isEncrypt)
{
#if UNITY_EDITOR
//Debug.Log("LoadXML");
#endif
return decrypt(dataString);
}
return dataString;
}
byte[] stringToUTF8ByteArray(string dataString)
{
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(dataString);
}
public bool hasFile(string fileName)
{
return File.Exists(fileName);
}
#endregion
#region Encrypt
//加密和解密所需要用的密钥,具体的数字可以自定义,但是必须一共是32位
byte[] _keyArray = UTF8Encoding.UTF8.GetBytes("12388578902329367887724456789119");
public string encrypt(string toEncrypt)
{
ICryptoTransform cTransfrom = getRijndaelManaged().CreateEncryptor();
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
byte[] resultArray = cTransfrom.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
public string decrypt(string toDecrypt)
{
ICryptoTransform cTransform = getRijndaelManaged().CreateDecryptor();
byte[] toDecryptArray = Convert.FromBase64String(toDecrypt);
byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
RijndaelManaged getRijndaelManaged()
{
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = _keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
return rDel;
}
#endregion
}
using UnityEngine;
using System;
using System.IO;
public abstract class XMLDataFileEncrypt
{
///
/// XML 文件名称
///
public abstract string FileName { get; }
///
/// XML 文件是否加密
///
public abstract bool IsXMLEncrypt { get; }
string fileDirPath
{
get
{
return Application.dataPath + Path.DirectorySeparatorChar + "Data" + Path.DirectorySeparatorChar;
}
}
///
/// 目录 地址
///
string dirPath
{
get
{
return Application.dataPath + Path.DirectorySeparatorChar + "Data";
}
}
///
/// 文件 地址
///
public string FilePath
{
get { return (fileDirPath + FileName + ".xml"); }
}
///
/// 生成 XML
///
///
public void SaveToXML() where T : XMLDataFileEncrypt
{
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
XmlManager.Instance.IsEncrypt = IsXMLEncrypt;
string serializedDataString = XmlManager.Instance.serializeObject(this, typeof(T));
XmlManager.Instance.createXML(FilePath, serializedDataString);
}
///
/// 生成 XML
///
public void SaveTo_XML()
{
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
XmlManager.Instance.IsEncrypt = IsXMLEncrypt;
string serializedDataString = XmlManager.Instance.serializeObject(this, this.GetType());
XmlManager.Instance.createXML(FilePath, serializedDataString);
}
///
/// 读取 XML
///
///
///
public T LoadFromXML() where T : XMLDataFileEncrypt
{
if (!File.Exists(FilePath))
{
SaveToXML();
}
if (XmlManager.Instance.hasFile(FilePath))
{
string dataString = XmlManager.Instance.loadXML(FilePath);
//Debug.Log(dataString);
T fromXML = XmlManager.Instance.deserializeObject(dataString, typeof(T)) as T;
//Debug.Log(fromXML.GetType());
if (fromXML != null)
{
// Debug.Log(FilePath);
// Debug.Log(fromXML);
return fromXML;
}
else
{
#if UNITY_EDITOR
Debug.Log("XMLData is null");
#endif
}
}
return null;
}
#if UNITY_EDITOR
#endif
}
下面来讲讲使用方法
这里需要注意继承 XMLDataFileEncrypt
好了,我们可以开始测试了
直接运行,会生成一个xml文件,如下
当我们按下s键,更改xml里面的数据,如下
强烈安利此插件,只需要几行代码就可以完成xml的存储,非常方便!
本次水文结束