- Home /
The question is answered, right answer was accepted
Saving and loading data from file
Hey,
I'm trying to save and load to/from a file. I've looked at this question and am trying to use it in my project to save the character's position. I'm really not sure how this works though..
public static void Save(string filePath)
{
SaveNLoadSerialize data = new SaveNLoadSerialize();
Stream stream = File.Open(filePath, FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();
bf.Binder = new VersionDeserializationBinder();
bf.Serialize(stream, data);
stream.Close();
}
In that piece of code, how does it know what data it should save?
Also..how do I access the save method from another script? I've tried making an object of the SaveNLoad class and call the save method, but not sure if that's the right way to do it.
Below is my full code:
using UnityEngine;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Reflection;
using System.IO;
using System;
using System.Text;
[Serializable()]
public class SaveNLoadSerialize : ISerializable {
public GameObject character;
public SaveNLoad snl = new SaveNLoad();
public float x;
public float y;
public float z;
public SaveNLoadSerialize()
{
}
public SaveNLoadSerialize(SerializationInfo info, StreamingContext ctx)
{
x = (float)info.GetValue("x", typeof(float));
y = (float)info.GetValue("y", typeof(float));
z = (float)info.GetValue("z", typeof(float));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctx)
{
info.AddValue("x", x);
info.AddValue("y", y);
info.AddValue("z", z);
}
public class SaveNLoad
{
public static string currentFilePath = "SaveNLoad.miko";
public void Save()
{
Debug.Log("saving");
Save (currentFilePath);
}
public static void Save(string filePath)
{
SaveNLoadSerialize data = new SaveNLoadSerialize();
Stream stream = File.Open(filePath, FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();
bf.Binder = new VersionDeserializationBinder();
bf.Serialize(stream, data);
stream.Close();
}
public void Load()
{
Debug.Log("Loading");
Load (currentFilePath);
}
public static void Load(string filePath)
{
SaveNLoadSerialize data = new SaveNLoadSerialize();
Stream stream = File.Open(filePath, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
bf.Binder = new VersionDeserializationBinder();
data = (SaveNLoadSerialize)bf.Deserialize(stream);
stream.Close();
Debug.Log(data.x);
}
}
public sealed class VersionDeserializationBinder : SerializationBinder
{
public override Type BindToType( string assemblyName, string typeName )
{
if ( !string.IsNullOrEmpty( assemblyName ) && !string.IsNullOrEmpty( typeName ) )
{
Type typeToDeserialize = null;
assemblyName = Assembly.GetExecutingAssembly().FullName;
// The following line of code returns the type.
typeToDeserialize = Type.GetType( String.Format( "{0}, {1}", typeName, assemblyName ) );
return typeToDeserialize;
}
return null;
}
}
I hope someone can help me with this.
nvm im stupid. need to add data.x = character.transform.position.x
to the save method
Follow this Question
Related Questions
Find SceneIndex out C# 1 Answer
Serialization/Deserialization Problems 0 Answers
,Block Levels and save progress 1 Answer
How can I continue the game without losing values after Game Over? 1 Answer
save load exp on database 2 Answers