unity3d里的XML读写示例
关于U3D里面XML读写办法,谢谢刘老师的指导(刘国栋)
代码:
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, "在一个精神病院里,有一天院长想看看三个精神病人的恢复情况如何,于是在他们每人面前放了一只小白兔。第一个精神病人坐在小白兔的上面,揪着小白兔的两只耳朵,嘴里嚷着“驾”,院长摇了摇头;"));
everyScrollData.Add(new ScrollData(false, "第二个人背对着小白兔,拍着它的屁股,嘴里说着“给我追”,院长叹了口气;"));
everyScrollData.Add(new ScrollData(false, "第三个蹲在那里一个劲儿的摸着小白兔,院长看后,满意地点点头,只听他说了一句:“小样的,放你300米,等我擦好车再追你!”院长倒地晕倒……"));
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, "苦海翻起爱恨 在世间难逃避命运 相亲竟不可接近 或我应该相信是缘份 苦海翻起爱恨 在世间难逃避命运 相亲竟不可接近 或我应该相信是缘份"));
LevelData.Add(new Data(0,true, "level1", everyScrollData[0], everyScrollData[1], everyScrollData[2]));
LevelData.Add(new Data(1,false, "level2", everyScrollData[3], everyScrollData[4], everyScrollData[5]));
LevelData.Add(new Data(2,false, "level3", everyScrollData[6], everyScrollData[7], everyScrollData[8]));
LevelData.Add(new Data(3,false, "level4", everyScrollData[9], everyScrollData[10], everyScrollData[11]));
LevelData.Add(new Data(4,false, "level5", everyScrollData[12], everyScrollData[13], everyScrollData[14]));
LevelData.Add(new Data(5,false, "level6", everyScrollData[15], everyScrollData[16], everyScrollData[17]));
LevelData.Add(new Data(6,false, "level7", everyScrollData[18], everyScrollData[19], everyScrollData[20]));
LevelData.Add(new Data(7,false, "level8", everyScrollData[21], everyScrollData[22], everyScrollData[23]));
LevelData.Add(new Data(8,false, "level9", everyScrollData[24], everyScrollData[25], everyScrollData[26]));
}
public static List<Data> getlistData()
{
Load ();
//TraverseList();
return LevelData;
}
public static string GetLevelName(int i)
{
return LevelData [i].LevelName;
}
}
相关文章