- Home /
[C#] Deserialization fails in Web Player
I store serialized data in PlayerPrefs in the Web Player. In the UNITY IDE all works fine, but deserialization fails in the Web Player.
This is my code for the serializer and the serializable class.
public class PlayerPrefsSerializer{
public static BinaryFormatter bf=new BinaryFormatter();
public static void Save(string key, object serializableObject){
if(serializableObject==null) return;
Debug.Log (string.Format ("Saving: {0} => {1}", key, serializableObject.GetType ()));
MemoryStream memoryStream=new MemoryStream();
bf.Serialize(memoryStream,serializableObject);
string serializedData=System.Convert.ToBase64String(memoryStream.ToArray());
PlayerPrefs.SetString(key, serializedData);
Debug.Log (string.Format ("Saved serialized data {0}", serializedData));
}
public static object Load(string key){
if(!PlayerPrefs.HasKey(key)){
Debug.Log ("Key not found");
return null;
}
string serializedData=PlayerPrefs.GetString(key,string.Empty);
if(serializedData==string.Empty){
Debug.Log ("No data for providad key");
return null;
}
Debug.Log (string.Format ("Loading serialized data {0} {1}", key, serializedData));
MemoryStream memoryStream=new MemoryStream(System.Convert.FromBase64String(serializedData));
object returnObject=bf.Deserialize(memoryStream) as List<PlayerData>;
Debug.Log (string.Format ("Loaded: {0} => {1}", key, returnObject.GetType ()));
//return null;
return returnObject;
}
}
[Serializable]
class PlayerData{
public int avatar{get;set;}
public string playerName{get;set;}
public PlayerData(int av, string pl){
avatar=av;
playerName=pl;
}
}
As mentioned above all works well in the UNITY IDE, but in the web player I get the following eror:
[Log] : Loading serialized data player
[Error] : Loading of Player information failed. Reason: System.MethodAccessException: Attempt to access a private/protected method failed.
at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (object,object[],System.Exception&)
at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
This means the player data is being loaded (I dont post that data here), but deserialization fails. (I think in line 28) Does anyone know why? Does it have to do with Unity Security issues regarding the Web Player?
Answer by FilouB · Sep 29, 2014 at 05:17 PM
Ok I solved the issue myself...
As the code broke in line 28 during deserialization I now serialize and deserialize using Json.NET. I also encode and decode the string as base64 to make it harder to mess with the PlayerPrefs file.
Serialization now works like this:
public static void Save(){
JsonSerializer serializer=new JsonSerializer();
serializer.NullValueHandling=NullValueHandling.Ignore;
StringWriter sw=new StringWriter();
using(JsonWriter writer=new JsonTextWriter(sw)){
writer.WriteStartObject();
writer.WritePropertyName("Players");
writer.WriteStartArray();
foreach(PlayerDetails player in players) SavePlayer(writer,player);
writer.WriteEndArray();
writer.WriteEndObject();
}
sw.Close();
StringBuilder bu = sw.GetStringBuilder();
string entiredata = bu.ToString();
string encodedData=EncodeTo64(entiredata);
PlayerPrefs.SetString("SaveGame", encodedData);
}
and deserialization like this:
public static void Load(){
players.Clear();
if(!PlayerPrefs.HasKey("SaveGame")){
return;
}
string encodedData=PlayerPrefs.GetString("SaveGame",string.Empty);
if(encodedData==string.Empty){
return;
}
Debug.Log ("Encoded savegame data from Playerprefs: "+encodedData);
string serializedData=DecodeFrom64(encodedData);
Debug.Log ("Decoded serialized savegame data from Playerprefs: "+serializedData);
if(serializedData!=null){
//parse content
using (JsonTextReader reader=new JsonTextReader(new StringReader(serializedData))){
while (reader.Read()){
if(reader.Value!=null){
if(reader.TokenType==JsonToken.PropertyName){
if((string)reader.Value=="Players")LoadPlayers(reader);
}
}
}
}
}
}
This works for me, as well in web player.
Your answer
![](https://koobas.hobune.stream/wayback/20220613163828im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
EndOfStreamException : Failed to read past end of stream 1 Answer
Why does deserialization fail in the web player 1 Answer
UnityWebRequest deserialize content 0 Answers
EndOfStreamException : Failed to read past end of stream 1 Answer
Using BinaryFormatter for deserialization - ArgumentException: method argument length mismatch 0 Answers