Unity Save/Load Script

      Unity Save/Load Script

      Evet arkadaşlar her zamanki gibi Buradanalıntı yaptım.İşte size script kolay gelsin;

      Source Code

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

      Post was edited 1 time, last by “Zantara” ().