Game Development Reference
In-Depth Information
This is achieved in Unity using data serialization via either XML or binary files:
Saving the Transform properties of an object to an XML file
Serialization is the process of converting, or translating, data in memory (such as
the state of a component on a GameObject ) into a stream that can be written to a
file and then loaded back from a file to recreate the component in memory, as it
was when it was saved. The process of creating a save-game, therefore, is about
first deciding which data must be saved and loaded (which is game specific) and
then creating a new class to hold that data. Consider the following code sample
10-4 ( ObjSerializer.cs ) that can be attached to any GameObject to serialize its
Transform component (Translation, Rotation, and Scale) to and from an external
file, both in the XML and binary formats. To achieve this, the XmlSerializer class
is used to convert an object in memory to XML, and the BinaryFormatter converts
an object in memory to a binary file. XML files are human-readable text files, while
binary files cannot normally be read or understood by humans:
001 //-----------------------------------------------
002 using UnityEngine;
003 using System.Collections;
 
Search WWH ::




Custom Search