unity3d里的XML读写示例

前端之家收集整理的这篇文章主要介绍了unity3d里的XML读写示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

关于U3D里面XML读写办法,谢谢刘老师的指导(刘国栋)


代码

/*
*Projectname:
*
*Author:
*
*Version:
*
*Description:
*
*/


usingUnityEngine;
usingSystem.Collections;
usingSystem.Xml;
usingSystem.Xml.Serialization;
usingSystem.IO;
usingSystem.Text;
usingSystem.Security.Cryptography;
usingSystem;
publicclassXmlSaver
{

privatestaticXmlSaver_Instance=null;
publicstaticXmlSaverGetInstance()
{
if(_Instance==null)
{
_Instance=newXmlSaver();
}
return_Instance;
}
//内容加密
publicstringEncrypt(stringtoE)
{
//加密和解密采用相同的key,具体自己填,但是必须为32位//
byte[]keyArray=UTF8Encoding.UTF8.GetBytes("12348578902223367877723456789012");
RijndaelManagedrDel=newRijndaelManaged();
rDel.Key=keyArray;
rDel.Mode=CipherMode.ECB;
rDel.Padding=PaddingMode.PKCS7;
ICryptoTransformcTransform=rDel.CreateEncryptor();

byte[]toEncryptArray=UTF8Encoding.UTF8.GetBytes(toE);
byte[]resultArray=cTransform.TransformFinalBlock(toEncryptArray,0,toEncryptArray.Length);

returnConvert.ToBase64String(resultArray,resultArray.Length);
}

//内容解密
publicstringDecrypt(stringtoD)
{
//加密和解密采用相同的key,具体值自己填,但是必须为32位//
byte[]keyArray=UTF8Encoding.UTF8.GetBytes("12348578902223367877723456789012");

RijndaelManagedrDel=newRijndaelManaged();
rDel.Key=keyArray;
rDel.Mode=CipherMode.ECB;
rDel.Padding=PaddingMode.PKCS7;
ICryptoTransformcTransform=rDel.CreateDecryptor();

byte[]toEncryptArray=Convert.FromBase64String(toD);
byte[]resultArray=cTransform.TransformFinalBlock(toEncryptArray,toEncryptArray.Length);

returnUTF8Encoding.UTF8.GetString(resultArray);
}

publicstringSerializeObject(objectpObject,System.Typety)
{
stringXmlizedString=null;
MemoryStreammemoryStream=newMemoryStream();
XmlSerializerxs=newXmlSerializer(ty);
XmlTextWriterxmlTextWriter=newXmlTextWriter(memoryStream,Encoding.UTF8);
xs.Serialize(xmlTextWriter,pObject);
memoryStream=(MemoryStream)xmlTextWriter.BaseStream;
XmlizedString=UTF8ByteArrayToString(memoryStream.ToArray());
returnXmlizedString;
}

publicobjectDeserializeObject(stringpXmlizedString,System.Typety)
{
XmlSerializerxs=newXmlSerializer(ty);
MemoryStreammemoryStream=newMemoryStream(StringToUTF8ByteArray(pXmlizedString));
XmlTextWriterxmlTextWriter=newXmlTextWriter(memoryStream,Encoding.UTF8);
returnxs.Deserialize(memoryStream);
}

//创建XML文件
publicvoidCreateXML(stringfileName,stringthisData)
{
//stringxxx=Encrypt(thisData);
stringxxx=thisData;
StreamWriterwriter;
writer=File.CreateText(fileName);
writer.Write(xxx);
writer.Close();
}

//读取XML文件
publicstringLoadXML(stringfileName)
{
StreamReadersReader=File.OpenText(fileName);
stringdataString=sReader.ReadToEnd();
sReader.Close();
//stringxxx=Decrypt(dataString);
stringxxx=dataString;
returnxxx;
}

//判断是否存在文件
publicboolhasFile(StringfileName)
{
returnFile.Exists(fileName);
}
publicstringUTF8ByteArrayToString(byte[]characters)
{
UTF8Encodingencoding=newUTF8Encoding();
stringconstructedString=encoding.GetString(characters);
return(constructedString);
}

publicbyte[]StringToUTF8ByteArray(StringpXmlString)
{
UTF8Encodingencoding=newUTF8Encoding();
byte[]byteArray=encoding.GetBytes(pXmlString);
returnbyteArray;
}
}

后面是加载数据:


代码


usingUnityEngine;
usingSystem.Collections;
usingSystem.Collections.Generic;
publicstaticclassDataLoadTest{
publicstaticList<Data>LevelData=newList<Data>();
//staticstring_fileName=Application.dataPath+"/Resource/gamedata";
staticstring_fileName=Application.persistentDataPath+"/gamedata";
publicstaticvoidSave()
{
strings=XmlSaver.GetInstance().SerializeObject(LevelData,typeof(List<Data>));
XmlSaver.GetInstance().CreateXML(_fileName,s);
}
publicstaticvoidLoad()
{
try{
strings=XmlSaver.GetInstance().LoadXML(_fileName);
LevelData=XmlSaver.GetInstance().DeserializeObject(s,typeof(List<Data>))asList<Data>;
}
catch{
TraverseList();
Save();
strings=XmlSaver.GetInstance().LoadXML(_fileName);
LevelData=XmlSaver.GetInstance().DeserializeObject(s,typeof(List<Data>))asList<Data>;
}

}
publicstaticvoidTraverseList()
{
List<ScrollData>everyScrollData=newList<ScrollData>();
everyScrollData.Add(newScrollData(false,"挖掘机学校哪家强"));
everyScrollData.Add(newScrollData(false,"中国山东找蓝翔"));
everyScrollData.Add(newScrollData(false,"我特喵的只是开发者用来测试的"));

everyScrollData.Add(newScrollData(false,"有我所不乐意的,在天堂里,我不愿意去;有我所不乐意的,在地狱里,我不愿意去;有我所不乐意的,在你们的世界里,我不愿意去。"));
everyScrollData.Add(newScrollData(false,"我只愿蓬勃生活在此时此刻,无所谓去哪,无所谓见谁。那些我将要去的地方,都是我从未谋面的故乡;那些我将要见的人,都会成为我的朋友。"));
everyScrollData.Add(newScrollData(false,"以前是以前,现在是现在。我不能选择怎么生,怎么死;但我能决定怎么爱,怎么活。这是我要的自由,我的黄金时代。"));

everyScrollData.Add(newScrollData(false,"栈和队列的区别是啥?"));
everyScrollData.Add(newScrollData(false,"吃多了拉就是队列;"));
everyScrollData.Add(newScrollData(false,"吃多了吐就是栈"));

everyScrollData.Add(newScrollData(false,"壮岁旌旗拥万夫,锦�突骑渡江初。燕兵夜�银胡�,汉箭朝飞金仆姑。"));
everyScrollData.Add(newScrollData(false,"追往事,叹今吾,春风不染白髭须。"));
everyScrollData.Add(newScrollData(false,"却将万字平戎策,换得东家种树书。"));

everyScrollData.Add(newScrollData(false,"第一天,小白兔去河边钓鱼,什么也没钓到,回家了。"));
everyScrollData.Add(newScrollData(false,"第二天,小白兔又去河边钓鱼,还是什么也没钓到,回家了。"));
everyScrollData.Add(newScrollData(false,"第三天,小白兔刚到河边,一条大鱼从河里跳出来,冲着小白兔大叫:你他妈的要是再敢用胡箩卜当鱼饵,我就扁死你!"));

everyScrollData.Add(newScrollData(false,"程序员的读书历程:x语言入门>x语言应用实践>x语言高阶编程>"));
everyScrollData.Add(newScrollData(false,"x语言的科学与艺术>编程之美>编程之道>"));
everyScrollData.Add(newScrollData(false,"编程之禅>颈椎病康复指南>精神病康复指南"));

everyScrollData.Add(newScrollData(false,"在一个精神病院里,有一天院长想看看三个精神病人的恢复情况如何,于是在他们每人面前放了一只小白兔。第一个精神病人坐在小白兔的上面,揪着小白兔的两只耳朵,嘴里嚷着“驾”,院长摇了摇头;"));
everyScrollData.Add(newScrollData(false,"第二个人背对着小白兔,拍着它的屁股,嘴里说着“给我追”,院长叹了口气;"));
everyScrollData.Add(newScrollData(false,"第三个蹲在那里一个劲儿的摸着小白兔,院长看后,满意地点点头,只听他说了一句:“小样的,放你300米,等我擦好车再追你!”院长倒地晕倒……"));

everyScrollData.Add(newScrollData(false,"创新无极限!只要敢想,没有什么不可能,立即跳出思维的框框吧。如果你正处于一个上升的朝阳行业,那么尝试去寻找更有效的解决方案:更招消费者喜爱、更简洁的商业模式。"));
everyScrollData.Add(newScrollData(false,"如果你处于一个日渐萎缩的行业,那么赶紧在自己变得跟不上时代之前抽身而出,去换个工作或者转换行业。不要拖延,立刻开始创新!"));
everyScrollData.Add(newScrollData(false,"--史蒂夫.乔布斯"));

everyScrollData.Add(newScrollData(false,"从前现在过去了再不来红红落叶长埋尘土内开始终结总是没变改天边的你飘泊白云外苦海翻起爱恨在世间难逃避命运相亲竟不可接近或我应该相信是缘份"));
everyScrollData.Add(newScrollData(false,"情人别后永远再不来无言独坐放眼尘世外鲜花虽会凋谢但会再开一生所爱隐约在白云外"));
everyScrollData.Add(newScrollData(false,"苦海翻起爱恨在世间难逃避命运相亲竟不可接近或我应该相信是缘份苦海翻起爱恨在世间难逃避命运相亲竟不可接近或我应该相信是缘份"));
LevelData.Add(newData(0,true,"level1",everyScrollData[0],everyScrollData[1],everyScrollData[2]));
LevelData.Add(newData(1,false,"level2",everyScrollData[3],everyScrollData[4],everyScrollData[5]));
LevelData.Add(newData(2,"level3",everyScrollData[6],everyScrollData[7],everyScrollData[8]));
LevelData.Add(newData(3,"level4",everyScrollData[9],everyScrollData[10],everyScrollData[11]));
LevelData.Add(newData(4,"level5",everyScrollData[12],everyScrollData[13],everyScrollData[14]));
LevelData.Add(newData(5,"level6",everyScrollData[15],everyScrollData[16],everyScrollData[17]));
LevelData.Add(newData(6,"level7",everyScrollData[18],everyScrollData[19],everyScrollData[20]));
LevelData.Add(newData(7,"level8",everyScrollData[21],everyScrollData[22],everyScrollData[23]));
LevelData.Add(newData(8,"level9",everyScrollData[24],everyScrollData[25],everyScrollData[26]));
}
publicstaticList<Data>getlistData()
{
Load();
//TraverseList();
returnLevelData;
}
publicstaticstringGetLevelName(inti)
{
returnLevelData[i].LevelName;
}
}

猜你在找的XML相关文章