在unity3D中使用xml

xy849288321發表於2012-11-15
在unity3D中使用xml的方法總結:

(使用c#)

1.需要引入一些包

[html] view plain
copy

using UnityEngine;
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Collections;

上面這些應該足夠了


2.建立unity3D中的一組資料結構

比如我想要存入某一個物體的運動引數
[html] view plain
copy

public class MovementArgument
{
public String Name;
public String Tag;
public float position_x;
public float position_y;
public float position_z;
public float velocity_x;
public float velocity_y;
public float velocity_z;
}


3.再使用一個類來儲存MovementArgument的物件,作為xml資料結構

[html] view plain
copy

public class UserData
{
public MovementArgument _iUser
public UserData()
{
_iUser = new MovementArgument();
}
}

4.建立使用於本資料結構的儲存讀取過程(在底層的xml操作之上)


讀取(其實讀取操作與資料結構無關了,可以複用)

[html] view plain
copy

public void LoadData()
{
StreamReader r = File.OpenText(_FileLocation+"/"+ _FileName);//_FileLocation是unity3D當前project的路徑名,_FileName是xml的檔名。定義為成員變數了
//當然,你也可以在前面先判斷下要讀取的xml檔案是否存在
String _data=r.ReadLine();
myData = DeserializeObject(_data) as UserData;//myData是上面自定義的xml存取過程中要使用的資料結構UserData
r.Close();
}
儲存(引數要根據需要變化)


[html] view plain
copy

public void SaveData(String sc1, String sc2,float sc3,float sc4,float sc5,float sc6,float sc7,float sc7)
{
tempData._iUser.Name = sc1;//tempData是成員變數,UserData型別
tempData._iUser.Tag = sc2;
tempData._iUser.position_x = sc3;
tempData._iUser.position_y = sc4;
tempData._iUser.position_z = sc5;
tempData._iUser.direction_x = sc6;
tempData._iUser.direction_z = sc7;
tempData._iUser.direction_y = sc8;

StreamWriter writer ;
FileInfo t = new FileInfo(_FileLocation+"/"+ _FileName);
t.Delete();
writer = t.CreateText();
String _data = SerializeObject(tempData);//序列化這組資料
writer.WriteLine(_data);//寫入xml
writer.Close();
}

5.底層的xml操作


底層xml讀寫使用.net中的System.xml名稱空間中的類和方法,具體可見http://msdn.microsoft.com/zh-cn/library/gg145036.aspx

[html] view plain
copy

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;
}

// Here we serialize our UserData object of myData
public String SerializeObject(object pObject)
{
String XmlizedString = "";
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(UserData));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, pObject);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream; // (MemoryStream)
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
return XmlizedString;
}

// Here we deserialize it back into its original form
public object DeserializeObject(String pXmlizedString)
{
XmlSerializer xs = new XmlSerializer(typeof(UserData));
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
return xs.Deserialize(memoryStream);
}

相關文章