Game Development Reference
In-Depth Information
{
//Create an in memory stream to hold our serialized
output
using (var stream = new MemoryStream())
{
//Serialize the data
serializer.Serialize(stream, input);
//Get the serialized output
output = stream.GetBuffer();
}
}
catch { }
//Return the serialized output
return output;
}
Note
I've implemented the serialization function using C# generics (type <T> ). This allows you
to build a function that will work for any type of class you supply it with. This saves us
from creating a serialization function for each and every type of data we want to serialize.
To learn more about generics (a fairly advanced topic), check out the MSDN documenta-
tion at http://msdn.microsoft.com/en-gb/library/512aeb7t.aspx .
Not all platforms support all serializers, and also, some classes (such as
MemoryStream ) are not available on all platforms. You will sometimes have to tailor
the approach you use to work with other platforms. If you do, however, make sure you do
it within the helper classes so that all the platform-variant code is in one place and does
not clutter up your game. More on supporting multiple platforms is covered in Chapter 12 ,
Deployment and Beyond .
The code is commented to explain what each step actually does. If you wish, you can
store the output of this function in PlayerPrefs . It's more likely, however, that you
will either save it to the Web or to a disk using a different buffer than MemoryStream
(see the following section). Other serializers work pretty much the same way using a dif-
ferent formatter (for example, binary serialization uses BinarySerialiser ).
Search WWH ::




Custom Search