- Home /
Can not save data to file and then load it in Unity3d
I want to save data of my game to file after starting the game, then after close/start game again I want to load data from that file. I tried to use this solution, but when I start and then stop simulation, I don't see file indicatorsInfo.dat
, but Debug.Log()
says, that it exists. Anyway it don't load the data, what is wrong?
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class GAMEMANAGERFileIO : MonoBehaviour
{
void OnEnable()
{
LoadFromFile();
SaveToFile();
}
void OnDisable()
{
SaveToFile();
}
public void SaveToFile()
{
GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER");
GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>();
BinaryFormatter binaryFormatter = new BinaryFormatter();
FileStream fileStream = File.Create(Application.persistentDataPath + "/indicatorsInfo.dat"); // open file
IndicatorsInfo indicatorsInfo = new IndicatorsInfo();
// Initialise data of the class IndicatorsInfo
//...
// save data to the file. Serialize([to where we want to save], [what we want to save])
binaryFormatter.Serialize(fileStream, indicatorsInfo);
fileStream.Close();
Debug.Log("saved"); // this works
}
public void LoadFromFile()
{
// check if file exisits before we will try to open it
if(File.Exists(Application.persistentDataPath + "/indicatorsInfo.dat"))
{
GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER");
GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>();
BinaryFormatter binaryFormatter = new BinaryFormatter();
FileStream fileStream = File.Open(Application.persistentDataPath + "/indicatorsInfo.dat", FileMode.Open); // open file
IndicatorsInfo indicatorsInfo = (IndicatorsInfo)binaryFormatter.Deserialize(fileStream); // read from file
fileStream.Close(); // close file
// Initialise local data with values from the class IndicatorsInfo
//...
Debug.Log("loaded"); // this works
}
}
}
// clear class with data, which we will store to file
[Serializable]
class IndicatorsInfo
{
//...
}
Answer by MechanicalGaming · Mar 19, 2016 at 11:11 PM
Try putting the game on standalone and then trying it out. Start the game, gain some points of what ever, quit the game and then start it back up to see what has changed! Unity simulation doesn't load the file.
I did'n know about that, thanks. But it still don't work
Well, if that is your code right there that you are using then you haven't told it to save anything. If you want it to save something you must put a float in it and all of that. Here's a video explaining it all: https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading
Watch the whole thing! It helps tons!
Answer by FireBoltStudios · Mar 21, 2016 at 09:56 AM
This is a script I have created that I use in many of my projects, it can save out to XML and Binary, and load. As long the class to be saved is not inheriting MonoBehaviour and uses primitive data types. very simple usage; for instance to load from a file the line would be.
MyClass myClass = Serializer.LoadXML(Path);
To save;
Serializer.SaveXML(Path,myClass);
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using System.Xml.Serialization;
namespace Assets.Scripts.Utility
{
public class Serialiser
{
public static T Load<T>(string filename) where T : class
{
if (File.Exists(filename))
{
using (Stream stream = File.OpenRead(filename))
{
BinaryFormatter formatter = new BinaryFormatter();
return formatter.Deserialize(stream) as T;
}
}
return null;
}
public static void Save<T>(string filename, T data) where T : class
{
using (Stream stream = File.Open(filename, FileMode.OpenOrCreate))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, data);
}
}
public static T LoadXML<T>(string filename) where T : class
{
if (File.Exists(filename))
{
using (Stream stream = File.OpenRead(filename))
{
XmlSerializer serial = new XmlSerializer(typeof(T));
return serial.Deserialize(stream) as T;
}
}
return null;
}
public static void SaveXML<T>(string filename, T data) where T : class
{
using (Stream stream = File.Open(filename, FileMode.OpenOrCreate))
{
XmlSerializer serial = new XmlSerializer(typeof (T));
serial.Serialize(stream, data);
}
}
}
}