unity3d操作XML(C#)

前端之家收集整理的这篇文章主要介绍了unity3d操作XML(C#)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

unity3D中使用xml方法总结:

(使用c#

1.需要引入一些包

@H_403_35@[html]view plaincopyprint?

1usingUnityEngine;

2usingSystem;

3usingSystem.Xml;

4usingSystem.Xml.Serialization;

5usingSystem.IO;

6usingSystem.Text;

7usingSystem.Collections;


上面这些应该足够了

2.创建unity3D中的一组数据结构

比如我想要存入某一个物体的运动参数

[html]view plaincopyprint?

8publicclass MovementArgument

9{

10 public String Name;

11 public String Tag;

12 public float position_x;

13 public float position_y;

14 public float position_z;

15 public float velocity_x;

16 public float velocity_y;

17 public float velocity_z;

18 }

3.再使用一个类来保存MovementArgument的对象,作为xml数据结构

[html]view plaincopyprint?

19 public class UserData

20 {

21 public MovementArgument _iUser

22 public UserData()

23 {

24 _iUser = new MovementArgument();

25 }

26 }


4.
创建使用于本数据结构的存储读取过程(在底层的xml操作之上)

读取(其实读取操作与数据结构无关了,可以复用)

[html]view plaincopyprint?

27 public void LoadData()

28 {

29 StreamReader r =File.OpenText(_FileLocation+"/"+ _FileName);//_FileLocationunity3D当前project的路径名,_FileNamexml文件名。定义为成员变量了

30 //当然,你也可以在前面先判断下要读取的xml文件是否存在

31 String _data=r.ReadLine();

32 myData = DeserializeObject(_data) asUserData;//myData是上面自定义xml存取过程中要使用的数据结构UserData

33 r.Close();

34 }

存储(参数要根据需要变化)

[html]view plaincopyprint?

35 public void SaveData(String sc1,Stringsc2,float sc3,float sc4,float sc5,float sc6,float sc7,float sc7)

36 {

37 tempData._iUser.Name = sc1;//tempData是成员变量,UserData类型

38 tempData._iUser.Tag = sc2;

39 tempData._iUser.position_x = sc3;

40 tempData._iUser.position_y = sc4;

41 tempData._iUser.position_z = sc5;

42 tempData._iUser.direction_x = sc6;

43 tempData._iUser.direction_z = sc7;

44 tempData._iUser.direction_y = sc8;

45

46 StreamWriter writer ;

47 FileInfo t = newFileInfo(_FileLocation+"/"+ _FileName);

48 t.Delete();

49 writer = t.CreateText();

50 String _data = SerializeObject(tempData);//序列化这组数据

51 writer.WriteLine(_data);//写入xml

52 writer.Close();

53 }


5.
底层的xml操作

底层xml读写使用.net中的System.xml命名空间中的类和方法,具体可见http://msdn.microsoft.com/zh-cn/library/gg145036.aspx

[html]view plaincopyprint?

54 public String UTF8ByteArrayToString(byte[]characters)

55 {

56 UTF8Encoding encoding = new UTF8Encoding();

57 String constructedString =encoding.GetString(characters);

58 return (constructedString);

59 }

60

61 public byte[] StringToUTF8ByteArray(StringpXmlString)

62 {

63 UTF8Encoding encoding = new UTF8Encoding();

64 byte []byteArray =encoding.GetBytes(pXmlString);

65 return byteArray;

66 }

67

68 // Here we serialize our UserData object ofmyData

69 public String SerializeObject(object pObject)

70 {

71 String XmlizedString = "";

72 MemoryStream memoryStream = new MemoryStream();

73 XmlSerializer xs = newXmlSerializer(typeof(UserData));

74 XmlTextWriter xmlTextWriter = newXmlTextWriter(memoryStream,Encoding.UTF8);

75 xs.Serialize(xmlTextWriter,pObject);

76 memoryStream =(MemoryStream)xmlTextWriter.BaseStream; // (MemoryStream)

77 XmlizedString =UTF8ByteArrayToString(memoryStream.ToArray());

78 return XmlizedString;

79 }

80

81 // Here we deserialize it back into its originalform

82 public object DeserializeObject(StringpXmlizedString)

83 {

84 XmlSerializer xs = newXmlSerializer(typeof(UserData));

85 MemoryStream memoryStream = newMemoryStream(StringToUTF8ByteArray(pXmlizedString));

86 XmlTextWriter xmlTextWriter = newXmlTextWriter(memoryStream,248)">87 return xs.Deserialize(memoryStream);

猜你在找的XML相关文章