- Home /
Save Load Data unity
I ve write this code, i tried to save and load Data into Unity. Here is my code
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Collections.Generic;
using System.Linq;
public class GameControl : MonoBehaviour
{
public static GameControl control;
public float health =10;
public float experience=10;
public Dictionary<string, object> Name_Dic = new Dictionary<string, object>()
{
{"laura",""},
{"steph ",""},
{"Ryan",""},
};
void Start()
{
control = this;
}
void OnGUI()
{
GUI.Label(new Rect(10,10,100,30),"Health"+health);
GUI.Label(new Rect(10,40,100,30),"Experience"+experience);
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath +"/playerInfo.dat");
PlayerData data = new PlayerData();
data.experience = experience;
data.health = health;
data.Name_Dic = Name_Dic;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if(File.Exists(Application.persistentDataPath+"/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath+"/playerInfo.dat",FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
health = data.health;
experience = data.experience;
Name_Dic = data.Name_Dic;
}
}
}
[Serializable]
class PlayerData
{
public float health;
public float experience;
public Dictionary<string, object> Name_Dic = new Dictionary<string, object>()
{
{"laura",""},
{"steph ",""},
{"Ryan",""},
};
public Dictionary<string, string> Dialogs_Dic = new Dictionary<string, string>()
{
{"A1", "Hello"},
{"A2", "Nice"},
{"A3", "Test"},
};
}
The problem now that when i click on save button , i save the health and experience int because when i click on load button i finc that these two variables are saved and loaded. My problem is with the dictionary, i can't know if when i click on button save the dictionary values are saved or not. How can i now if the dictionary values are saved when i click on function saved and loaded if i click on button load.Is there a solution for that, thanks for your help
You could go through each $$anonymous$$eyValuePair and list it.
Hello,
Thanks for your answer . If i added keyvaluepair , in the Load function it is after the Comment part in the code. when i cliick on Save button in the code and i click on load button i receive "laura","",, "steph ","", "Ryan","", as response. So the dictionary has been saved and loaded?is that true? Thaks for your answer
public void Load()
{
if(File.Exists(Application.persistentDataPath+"/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath+"/playerInfo.dat",File$$anonymous$$ode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
health = data.health;
experience = data.experience;
Name_Dic = data.Name_Dic;
// Added Code
foreach ($$anonymous$$eyValuePair<string, object> pair in data.Name_Dic)
{
Debug.Log(String.Format("{0}, {1}", pair.$$anonymous$$ey, pair.Value));
}
}
}
Yes, the dictionary has been loaded successfully :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612072820im_/https://answers.unity.com/themes/thub/images/avi.jpg)