您好,登錄后才能下訂單哦!
關于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, "在一個精神病院里,有一天院長想看看三個精神病人的恢復情況如何,于是在他們每人面前放了一只小白兔。第一個精神病人坐在小白兔的上面,揪著小白兔的兩只耳朵,
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。