Saving Game Data to iOS Device

I have been attempting to save a serializable class to the persistent data path on iOS, but I am running into issues with BinaryFormatter. **I do not want to use PlayerPrefs** to store game data, as I don't want to be limited to just strings, ints, and floats.

Here's what I have setup:

  • I’m using the data persistence scripts from Unity’s Adventure Game Tutorial (SaveData, Saver, ResettableScriptableObject, etc). These work beautifully when persisting data between scenes, just as the tutorial had shown.
  • I have created a static and abstract SaveDataManager class that is responsible for saving/loading the SaveData to/from the persistent data path on disk.
  • As countless Google searches have advised me, I am using a BinaryFormatter to serialize and deserialize the SaveData type.
  • I’m building out my game to Xcode as a Development Build. I’m using Xcode 8.3 running on macOS Sierra 10.12.4, an iPhone 7 running iOS 10.3, and Unity 5.6.0b11 Personal.

Unfortunately, it has come to my attention that iOS does not support BinaryFormatters. I kept getting an error message saying "**SerializationException: Unexpected binary element: 255**". The issue was present in both the build *and* in the Editor. So, after some additional searching, I learned about the following line of code:

Environment.SetEnvironmentVariable ("MONO_REFLECTION_SERIALIZER", "yes");

I was advised to add it to an Awake function, and then everything should work. Ideally, I want the SaveDataManager to be abstract, but at that point I was desperate just to get it to work at all. Even after deriving it from Monobehaviour, adding it to the scene, and adding that line of code to the Awake function, I still kept getting the error. I even tried keeping it abstract and adding it to both the save and load functions, but the issue was still ongoing.

Desperate, I caved and decided to just try using PlayerPrefs anyways. So here's what I did:

  • I used PlayerPrefs.SetInt to set a score, determined the high score, then did the same thing for it.
  • I ensured that the keys I made were identical.
  • I then called PlayerPrefs.Save to actually save that information.

I then built the game out to Xcode and ran it on my iPhone, but the data was not being saved after I quit the app. Again though, I would rather not use this method anyways as I don’t have as much control over what can be saved.

Can anyone suggest another way to do this? Are there other ways to serialize and save data that *are* supported by iOS? Am I missing something? I've pasted my SaveDataManager script for reference.

Thanks so much!


using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using System.IO;

public static class SaveDataManager
{
	private const string FILENAME = "/save.data";

	public static void SaveToPlatform (SaveData saveData)
	{
		BinaryFormatter bf	= new BinaryFormatter();
		FileStream stream	= new FileStream(Application.persistentDataPath + FILENAME, FileMode.Create);

		bf.Serialize(stream, saveData);
		stream.Close();
	}

	public static SaveData LoadFromPlatform ()
	{
		if (File.Exists(Application.persistentDataPath + FILENAME))
		{
			BinaryFormatter bf	= new BinaryFormatter();
			FileStream stream	= new FileStream(Application.persistentDataPath + FILENAME, FileMode.Open);
			SaveData data		= bf.Deserialize(stream) as SaveData;

			stream.Close();
			return data;
		}
		else
		{
			SaveData data = new SaveData();
			SaveToPlatform(data);
			return data;
		}
	}
}

Ladies and gentlemen, I have found the answer.

I did 3 things:

    • I added back the line
    Environment.SetEnvironmentVariable ("MONO_REFLECTION_SERIALIZER", "yes"); to both functions.
  • I modified the SaveData script provided by Unity to not use the Editor at all, which is fine for what I’m using it for.
  • I created an XML file called link.xml, which contained the classes I wanted to be excluded from stripping.

The reason why the last point is necessary is because Unity strips down the code to reduce the file size and efficiency of the build. However, this also means that some classes necessary to your project get included in that process. In this case, the Binary Formatter class was being stripped from my code, thus not allowing iOS to read it properly. The linker file, which I found out about [here][1], excluded that class from the stripping process. I also added the System.IO class just to be absolutely sure.