Hi. I have a serious problem with the serialization of a list of my custom class (LevelInfo) inside of the container class (Information) I want to serialize. I’m using the simple state saving script.
The minor problem is, that the private fields of my class do not get serialized.
The main problem is, that when I save Information class and then load it again, the Levelinfo list gets another set of fresh entries. This results in clogging the list with unusable information and I have no clue what went wrong.
Any help is higly appreciated. Thanks in advance.
LevelInfo class:
[System.Serializable]
public class LevelInfo{
[SerializeField]
private float levelTime; //Does not get serialized, despite of [serializefield]
public float parTime; //This does. Set to public for testing purpose
//...
public LevelInfo(){ *** } //Blank Constructor
public LevelInfo(float parTime){ ***} // other constructor
public float ParTime{ get{return parTime;}}
public bool BeatPartime{get{return (levelTime <= parTime);}}
public void BeatLevel(float time){levelTime = Mathf.Min(levelTime, time);}
//...
}
Information class:
[System.Serializable]
public class Information{
[SerializeField]
public List<LevelInfo> levels;
//...
//This is inside of the Information constructor.
//This is the only place I use any LevelInfo constructor.
private void CreateLevelList(){
levels = new List<LevelInfo>();
levels.Add(new LevelInfo(4.0f));
//...
}
}
InformationTransport class:
public class InformationTransport: MonoBehaviour {
public Information info;
void Awake () {
LoadInfo();
}
private void SaveInfo(){
StateStorage.SaveData<Information>(saveKey, info);
}
private void LoadInfo(){
Information newInfo = StateStorage.LoadData<Information>(saveKey);
if(newInfo == null)
info = new Information();
else
info = newInfo;
}
StateStorage class:
public class StateStorage{
public static T LoadData<T>( string key ){
if ( PlayerPrefs.HasKey( key ) ) {
XmlSerializer serializer = new XmlSerializer( typeof( T ) );
Debug.Log(PlayerPrefs.GetString( key ));
StringReader sr = new StringReader( PlayerPrefs.GetString( key ) );
return ( T )serializer.Deserialize( sr );
}else{
return default(T);
}
}
public static void SaveData<T>( string key, T source ){
XmlSerializer serializer = new XmlSerializer( typeof( T ) );
StringWriter sw = new StringWriter();
serializer.Serialize( sw, source );
Debug.Log(sw.ToString());
PlayerPrefs.SetString( key, sw.ToString() );
PlayerPrefs.Save();
}
public static void ClearData( string key ){
PlayerPrefs.DeleteKey( key );
}
}