unity3d里的XML读写示例
关于U3D里面XML读写办法,谢谢刘老师的指导(刘国栋)
代码:
/*
*Project name:
*
*Author:
*
*Version:
*
*Description:
*
*/
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
{
private static XmlSaver _Instance = null;
public static XmlSaver GetInstance()
{
if (_Instance == null)
{
_Instance = new XmlSaver();
}
return _Instance;
}
//内容加密
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,0,toEncryptArray.Length);
return Convert.ToBase64String(resultArray,0,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,0,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);
string xxx = 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);
string xxx = 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;
}
}
后面是加载数据:
代码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class DataLoadTest{
public static List<Data> LevelData = new List<Data>();
//static string _fileName = Application.dataPath + "/Resource/gamedata";
static string _fileName = Application.persistentDataPath + "/gamedata";
public static void Save()
{
string s = XmlSaver.GetInstance().SerializeObject(LevelData, typeof(List<Data>));
XmlSaver.GetInstance().CreateXML(_fileName, s);
}
public static void Load()
{
try{
string s = XmlSaver.GetInstance().LoadXML(_fileName);
LevelData = XmlSaver.GetInstance().DeserializeObject(s, typeof(List<Data>)) as List<Data>;
}
catch {
TraverseList();
Save();
string s = XmlSaver.GetInstance().LoadXML(_fileName);
LevelData = XmlSaver.GetInstance().DeserializeObject(s, typeof(List<Data>)) as List<Data>;
}
}
public static void TraverseList()
{
List<ScrollData> everyScrollData = new List<ScrollData>();
everyScrollData.Add(new ScrollData(false, "挖掘机学校哪家强"));
everyScrollData.Add(new ScrollData(false, "中国山东找蓝翔"));
everyScrollData.Add(new ScrollData(false, "我特喵的只是开发者用来测试的"));
everyScrollData.Add(new ScrollData(false, "有我所不乐意的,在天堂里,我不愿意去;有我所不乐意的,在地狱里,我不愿意去; 有我所不乐意的,在你们的世界里,我不愿意去。 "));
everyScrollData.Add(new ScrollData(false, "我只愿蓬勃生活在此时此刻, 无所谓去哪,无所谓见谁。 那些我将要去的地方,都是我从未谋面的故乡; 那些我将要见的人,都会成为我的朋友。"));
everyScrollData.Add(new ScrollData(false, "以前是以前,现在是现在。 我不能选择 怎么生,怎么死; 但我能决定 怎么爱,怎么活。这是我要的*,我的黄金时代。 "));
everyScrollData.Add(new ScrollData(false, "栈和队列的区别是啥?"));
everyScrollData.Add(new ScrollData(false, "吃多了拉就是队列;"));
everyScrollData.Add(new ScrollData(false, "吃多了吐就是栈"));
everyScrollData.Add(new ScrollData(false, "壮岁旌旗拥万夫,锦襜突骑渡江初。燕兵夜娖银胡簶,汉箭朝飞金仆姑。"));
everyScrollData.Add(new ScrollData(false, "追往事,叹今吾,春风不染白髭须。"));
everyScrollData.Add(new ScrollData(false, "却将万字平戎策,换得东家种树书。"));
everyScrollData.Add(new ScrollData(false, "第一天,小白兔去河边钓鱼,什么也没钓到,回家了。 "));
everyScrollData.Add(new ScrollData(false, "第二天,小白兔又去河边钓鱼,还是什么也没钓到,回家了。"));
everyScrollData.Add(new ScrollData(false, "第三天,小白兔刚到河边,一条大鱼从河里跳出来,冲着小白兔大叫:你他妈的要是再敢用胡箩卜当鱼饵,我就扁死你!"));
everyScrollData.Add(new ScrollData(false, "程序员的读书历程:x 语言入门 —> x 语言应用实践 —> x 语言高阶编程 —> "));
everyScrollData.Add(new ScrollData(false, "x 语言的科学与艺术 —> 编程之美 —> 编程之道 —> "));
everyScrollData.Add(new ScrollData(false, "编程之禅 —> 颈椎病康复指南 —> 精神病康复指南 "));
everyScrollData.Add(new ScrollData(false, "在一个精神病院里,有一天院长想看看三个精神病人的恢复情况如何,于是在他们每人面前放了一只小白兔。第一个精神病人坐在小白兔的上面,揪着小白兔的两只耳朵,