- Home /
Issue loading serialized config.dat from persistant Data
So Im trying to save a config file to persistantData im not getting any errors but my loading is returning "null" and im not sure why. Im new to serializing. I have 3 classes.
config Data Class
Static Dictionary
Loading Class
Heres my data and dictionary
public class config : MonoBehaviour
{
public int id;
public string textVal;
public int intVal;
public float floatVal;
public bool boolVal;
}
//this class stores all the data
public static class ConfigData
{
public static SortedDictionary<string ,config> configData = new SortedDictionary<string , config>();
}
compling with no errors. Then theres my saving and file check methods.
//check if file exists and preform appropriate action
public UILabel txt;
// Use this for initialization
void Start ()
{
configFile=ConfigData.configData;
if(!File.Exists(Application.persistentDataPath + "/config.dat"))
{
Debug.Log("config file does not exist, It will be created");
txt.text = "Creating Config file";
createConfig();
}
else
{
txt.text = "Loading Config File";
loadConfig();
//Binary formatter for loading back
}
}
again no errors and it identifies if the file exists correctly. I save as follows.
//create the config file
void createConfig()
{
var b=new BinaryFormatter();
var f=File.Create(Application.persistentDataPath + "/config.dat");
createData();
b.Serialize(f,ConfigData.configData);
f.Close();
}
//create the data
void createData()
{
config tempconfig=new config();
tempconfig.id=0;
tempconfig.textVal="0.0.1";
ConfigData.configData.Add("Version" , tempconfig);
}
and finally here's loading
void loadConfig()
{
var b = new BinaryFormatter();
//Get the file
var f = File.Open(Application.persistentDataPath + "/config.dat", FileMode.Open);
ConfigData.configData = b.Deserialize(f) as SortedDictionary<string, config>;
f.Close();
Debug.Log(ConfigData.configData);
}
I checked the config.dat file and I think its saving properly
ÿÿÿÿ verson 0.0.1
but i cant be 100% certain as its serialized. but this tell me it has to be in how im loading the data. Any Help is greatly appreciated!
If you deserialize to an object - is it null? If not, what type is it?
var o = b.Deserialize(f);
Debug.Log(o.GetType());
Is there a good reason to use a SortedDictionary? - those things suck at performance...
You can also use my Unity Serializer to do this kind of stuff, the basic part of it is a BinaryFormatter replacement that handles some Unity objects better - it can deserialize any IDictionary derivative.