Hi there,
Usually i serialize my savegames to a file and this works yust fine and is super convenient, i just put my class [system.Serializable] attribute and can save it.
So now i need to deploy to Webplayer and i can't save a file, so i thought of encoding my serialized file to Base64 and save it to playerprefs. This works fine in Editor and Standalone, and saving even works in Webplayer, BUT when i try to deserialize my string/savegame it does not work and i get this error in Webplayer log:
SaveLoad.LoadFileFromPP(): Failed autosave (Reason: System.FieldAccessException: Attempt to access a private/protected field failed.
Any Ideas on how to fix this? I really want to avoid switching all my Savegame stuff to some json serialisation or whatever...
this are the relevant code parts:
**Saving** (Works, no error in Webplayer):
public static void SaveFileToPlayerprefs(string filename, object obj){
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, obj);
string str = System.Convert.ToBase64String(memoryStream.ToArray ());
PlayerPrefs.SetString ( filename, str);
}
**Loading** (this does work in Editor but **not in Webplayer**, with "SaveGame" beeing my custom class which holds all the serialized classes which need to be saved):
public static SaveGame LoadFileFromPlayerPrefs (string filename)
{
try {
BinaryFormatter binaryFormatter = new BinaryFormatter();
string tmp = PlayerPrefs.GetString (filename, string.Empty);
if (tmp == string.Empty )
return null;
MemoryStream memoryStream = new MemoryStream (System.Convert.FromBase64String (tmp));
return binaryFormatter.Deserialize (memoryStream) as SaveGame;
}
catch(Exception e) {
Debug.LogError("SaveLoad.LoadFileFromPP(): Failed " + filename + " (Reason: " + e.ToString() + ")");
return null;
}
}
Thanks for any advice/ideas!
Simon
↧