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?