Quiz Data class:
Code: Select all
class QuizData
{
private CategoryData _data;
private void Save()
{
ES3.Save("QuizData", _data);
Debug.Log("Saved Bytes: " + _data.Serialize().Length);
}
private void Load()
{
_data = ES3.Load<CategoryData>("QuizData");
Debug.Log("Loaded Bytes: " +_data.Serialize().Length);
}
[Serializable]
private class CategoryData : BuiltInDictionary<string, LevelData> { }
[Serializable]
private class LevelData : BuiltInDictionary<int, QuestionData> { }
[Serializable]
private class QuestionData
{
public void SetSolved()
{
Solved = true;
}
public bool Solved { get; internal set; }
}
Code: Select all
[Serializable]
public class BuiltInDictionary<TKey, TValue>
{
public Dictionary<TKey, TValue> Dictionary { get; } = new Dictionary<TKey, TValue>();
public void AddToDictionary(TKey key, TValue value)
{
Dictionary[key] = value;
}
}
Code: Select all
public static byte[] Serialize(this object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
public static T Deserialize<T>(this byte[] byteArray)
{
if (byteArray == null || byteArray.Length == 0)
return default;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream(byteArray))
{
return (T)bf.Deserialize(ms);
}
}
Code: Select all
private void Save()
{
Debug.Log("Saved Bytes: " + _data.Serialize());
ES3.Save("QuizData", _data.Serialize());
}
private void Load()
{
_data = ES3.Load<byte[]>("QuizData").Deserialize<CategoryData>();
Debug.Log("Loaded Bytes: " +_data.Serialize().Length);
}
Pic2: