Game Development Reference
In-Depth Information
2. Create a save file using Unity's File function (passing in the path to its loca-
tion).
3. Create a serialized copy of our player in a new PlayerSaveState property.
4. Finally, we write our serialized data to our save file.
Tip
With any operation that writes data outside of your game, always wrap it in a try / catch
block. This will ensure your game doesn't crash when one out of a million bad things
could happen.
This is all very simple. Then, to retrieve the saved data from the disk, first we'll add a
little helper function to tell us whether a save file already exists, which we can also use
elsewhere in the game, as follows:
public static bool SaveAvailable
{
get { return File.Exists(saveFilePath); }
}
This just uses another function of the File class to test the existence of a file. Now, we
can add the Load method as follows:
public static void LoadState(Action LoadComplete)
{
try
{
if (SaveAvailable)
{
//Get the file
using (var stream = File.Open(saveFilePath,
FileMode.Open))
{
var LoadedPlayer =
SerializerHelper.DeSerialise<PlayerSaveState>
(stream);
currentPlayer =
Search WWH ::




Custom Search