Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundaments of Game Design

Similar presentations


Presentation on theme: "Fundaments of Game Design"— Presentation transcript:

1 Fundaments of Game Design
File IO and Serialization Richard Gesick

2 Objectives Reading and writing files Data path vs persistent data path
Serialization of data

3 File Input / Output Consider that a program may want to make its data persistent (or not rely upon the user for input) Reading from and writing to a file is conceptually the same as reading/writing to the console

4 Streams Streams hold data
Input streams provide data to the program (keyboard and ReadLine) The program places data onto output streams (console and WriteLine)

5 Process: Open the file stream
(many "modes" - read, write, append, create, etc.) Do what you need to do (read/write) Close the file stream (freeing it for use by other programs) You can imagine that there is an "active cursor" that is moved around the file as you read/write

6 Opening / Closing Files
Opening the file StreamReader sr = new StreamReader("data.txt"); StreamWriter sw = new StreamWriter("output.txt"); Closing the file sr.Close(); sw.Close();

7 Reading / Writing data Reading data from the file
int x = Int32.Parse(sr.ReadLine()); or while (!sr.EndOfStream) { Console.WriteLine(sr.ReadLine()); } Writing data to the file sw.WriteLine(42);

8 In Unity In order to read a file from the Unity assets folder, use Application.dataPath + the file name rather than just the file name. sr= new StreamReader(“Application.dataPath”+”/”+filename); You can also use Application.persistentDataPath which stores the file in the “hidden” company name folder inside the LocalLow folder inside the App Data folder.

9 Serialization Sometimes it is easier to read or write entire objects than to read and write individual fields. C# provides such a mechanism, called object serialization. A serialized object is an object represented as a sequence of bytes that includes the object’s data, its type and the types of data stored in the object. After a serialized object has been written to a file, it can be read from the file and deserialized.

10 Serialization you probably won't be able to read it via a text editor
If you are writing binary information into a file, you probably won't be able to read it via a text editor Works on objects, but the class must be marked as serializable (and is recursive)

11 Serialization Class BinaryFormatter enables entire objects to be written to or read from a stream. BinaryFormatter method Serialize writes an object’s representation to a file. BinaryFormatter method Deserialize reads this representation from a file and reconstructs the original object. Both methods throw a SerializationException if an error occurs during serialization or deserialization.

12 Creating a File Using Object Serialization
The classes for objects that we wish to serialize must include this attribute in their declarations or must implement interface ISerializable. In a serializable class, you must ensure that every instance variable of the class is also serializable.

13 Creating a File Using Object Serialization
All simple-type variables and strings are serializable. For variables of reference types, their types must be serializable. By default, array objects are serializable. However, if the array contains references to other objects, those objects may or may not be serializable.


Download ppt "Fundaments of Game Design"

Similar presentations


Ads by Google