01.import System;
02.import System.Collections;
03.import System.Xml;
04.import System.Xml.Serialization;
05.import System.IO;
06.import System.Text;
07.// Anything we want to store in the XML file, we define it here
08.class DemoData
09.{
10. var x : float;
11. var y : float;
12. var z : float;
13. var name : String;
14.}
15.// UserData is our custom class that holds our defined objects we want to store in XML format
16. class UserData
17. {
18. // We have to define a default instance of the structure
19. public var _iUser : DemoData = new DemoData();
20. // Default constructor doesn't really do anything at the moment
21. function UserData() { }
22.}
23.//public class GameSaveLoad: MonoBehaviour {
24.// An example where the encoding can be found is at
25.// http://www.eggheadcafe.com/articles/system.xml.xmlserialization.asp
26.// We will just use the KISS method and cheat a little and use
27.// the examples from the web page since they are fully described
28.// This is our local private members
29.private var _Save : Rect;
30.private var _Load : Rect;
31.private var _SaveMSG : Rect;
32.private var _LoadMSG : Rect;
33.//var _ShouldSave : boolean;
34.//var _ShouldLoad : boolean;
35.//var _SwitchSave : boolean;
36.//var _SwitchLoad : boolean;
37.private var _FileLocation : String;
38.private var _FileName : String = "SaveData.xml";
39.//public GameObject _Player;
40.var _Player : GameObject;
41.var _PlayerName : String = "Joe Schmoe";
42.private var myData : UserData;
43.private var _data : String;
44.private var VPosition : Vector3;
45.// When the EGO is instansiated the Start will trigger
46.// so we setup our initial values for our local members
47.//function Start () {
48.function Awake () {
49. // We setup our rectangles for our messages
50. _Save=new Rect(10,80,100,20);
51. _Load=new Rect(10,100,100,20);
52. _SaveMSG=new Rect(10,120,200,40);
53. _LoadMSG=new Rect(10,140,200,40);
54.
55. // Where we want to save and load to and from
56. _FileLocation=Application.dataPath;
57.
58.
59. // we need soemthing to store the information into
60. myData=new UserData();
61. }
62.
63.function Update () {}
64.
65.function OnGUI()
66.{
67. // ***************************************************
68. // Loading The Player...
69. // **************************************************
70. if (GUI.Button(_Load,"Load")) {
71.
72. GUI.Label(_LoadMSG,"Loading from: "+_FileLocation);
73. // Load our UserData into myData
74. LoadXML();
75. if(_data.ToString() != "")
76. {
77. // notice how I use a reference to type (UserData) here, you need this
78. // so that the returned object is converted into the correct type
79. //myData = (UserData)DeserializeObject(_data);
80. myData = DeserializeObject(_data);
81. // set the players position to the data we loaded
82. VPosition=new Vector3(myData._iUser.x,myData._iUser.y,myData._iUser.z);
83. _Player.transform.position=VPosition;
84. // just a way to show that we loaded in ok
85. Debug.Log(myData._iUser.name);
86. }
87.
88. }
89.
90. // ***************************************************
91. // Saving The Player...
92. // **************************************************
93. if (GUI.Button(_Save,"Save")) {
94.
95. GUI.Label(_SaveMSG,"Saving to: "+_FileLocation);
96. //Debug.Log("SaveLoadXML: sanity check:"+ _Player.transform.position.x);
97.
98. myData._iUser.x = _Player.transform.position.x;
99. myData._iUser.y = _Player.transform.position.y;
100. myData._iUser.z = _Player.transform.position.z;
101. myData._iUser.name = _PlayerName;
102.
103. // Time to creat our XML!
104. _data = SerializeObject(myData);
105. // This is the final resulting XML from the serialization process
106. CreateXML();
107. Debug.Log(_data);
108. }
109.}
110.
111./* The following metods came from the referenced URL */
112.//string UTF8ByteArrayToString(byte[] characters)
113.function UTF8ByteArrayToString(characters : byte[] )
114.{
115. var encoding : UTF8Encoding = new UTF8Encoding();
116. var constructedString : String = encoding.GetString(characters);
117. return (constructedString);
118.}
119.//byte[] StringToUTF8ByteArray(string pXmlString)
120.function StringToUTF8ByteArray(pXmlString : String)
121.{
122. var encoding : UTF8Encoding = new UTF8Encoding();
123. var byteArray : byte[] = encoding.GetBytes(pXmlString);
124. return byteArray;
125.}
126.
127. // Here we serialize our UserData object of myData
128. //string SerializeObject(object pObject)
129.function SerializeObject(pObject : Object)
130.{
131. var XmlizedString : String = null;
132. var memoryStream : MemoryStream = new MemoryStream();
133. var xs : XmlSerializer = new XmlSerializer(typeof(UserData));
134. var xmlTextWriter : XmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
135. xs.Serialize(xmlTextWriter, pObject);
136. memoryStream = xmlTextWriter.BaseStream; // (MemoryStream)
137. XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
138. return XmlizedString;
139.}
140. // Here we deserialize it back into its original form
141. //object DeserializeObject(string pXmlizedString)
142.function DeserializeObject(pXmlizedString : String)
143.{
144. var xs : XmlSerializer = new XmlSerializer(typeof(UserData));
145. var memoryStream : MemoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
146. var xmlTextWriter : XmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
147. return xs.Deserialize(memoryStream);
148.}
149. // Finally our save and load methods for the file itself
150.function CreateXML()
151.{
152. var writer : StreamWriter;
153. //FileInfo t = new FileInfo(_FileLocation+"\\"+ _FileName);
154. var t : FileInfo = new FileInfo(_FileLocation+"/"+ _FileName);
155. if(!t.Exists)
156. {
157. writer = t.CreateText();
158. }
159. else
160. {
161. t.Delete();
162. writer = t.CreateText();
163. }
164. writer.Write(_data);
165. writer.Close();
166. Debug.Log("File written.");
167.}
168.
169.function LoadXML()
170.{
171. //StreamReader r = File.OpenText(_FileLocation+"\\"+ _FileName);
172. var r : StreamReader = File.OpenText(_FileLocation+"/"+ _FileName);
173. var _info : String = r.ReadToEnd();
174. r.Close();
175. _data=_info;
176. Debug.Log("File Read");
177.}
178.//}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhulinpptor/archive/2010/09/07/5868654.aspx
JS读取XML
最新推荐文章于 2022-09-27 15:01:58 发布