Using XML technology in Unity

1. Some packages need to be introduced

[html]view plaincopyprint?

using UnityEngine;

using System;

using System.Xml;

using System.Xml.Serialization;

using System.IO;

using System.Text;

using System.Collections;


The above should be enough

2. Create a set of data structures in unity3D

For example, I want to store the motion parameters of an object

[html]view plaincopyprint?

public class MovementArgument

{

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. Use another class to save the MovementArgument object as an xml data structure

[html]view plaincopyprint?

19 public class UserData

20 {

21 public MovementArgument _iUser

22 public UserData()

23 {

24 _iUser = new MovementArgument();

25 }

26 }


4. Create a stored read process for this data structure (on top of the underlying xml operations)

Read (in fact, the read operation has nothing to do with the data structure and can be reused)

[html]view plaincopyprint?

27 public void LoadData()

28 {

29  StreamReader r = File.OpenText(_FileLocation+"/"+ _FileName);//_FileLocation is the path name of the current project of unity3D , and _FileName is the file name of xml . Defined as a member variable

30  // Of course, you can also judge whether the xml file to be read exists in the front

31 String _data=r.ReadLine();

32  myData = DeserializeObject(_data) as UserData;//myData is the data structure UserData to be used in the custom xml access process above

33 r.Close();

34 }

Storage (parameters should be changed as needed)

[html]view plaincopyprint?

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

36 {

37  tempData._iUser.Name = sc1;//tempData is a member variable, UserData type

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 = new FileInfo(_FileLocation+"/"+ _FileName);

48 t.Delete();

49 writer = t.CreateText();

50  String _data = SerializeObject(tempData);// Serialize this set of data

51  writer.WriteLine (_data); // Imported xml

52 writer.Close();

53 }


5. The underlying xml operation

The underlying xml read and write uses the classes and methods in the System.xml namespace in .net , see http://msdn.microsoft.com/zh-cn/library/gg145036.aspx for details

[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(String pXmlString)

62 {

63 UTF8Encoding encoding = new UTF8Encoding();

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

65 return byteArray;

66 }

67 

68 // Here we serialize our UserData object of myData

69 public String SerializeObject(object pObject)

70 {

71 String XmlizedString = "";

72 MemoryStream memoryStream = new MemoryStream();

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

74 XmlTextWriter xmlTextWriter = new XmlTextWriter(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 original form

82 public object DeserializeObject(String pXmlizedString)

83 {

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

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

86 XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

87 return xs.Deserialize(memoryStream);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324650447&siteId=291194637