- Home /
How To Save Load to Hard Disc Drive C#
using Unity 4.0.1,
Windows 7
C#
I've found this but when I give in first code I start getting errors
Assets/Scripts/Class/SaveData.cs(13,18): error CS0535: `SaveData' does not implement interface member `System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)'
than if I modify it a bit - following his path don't know why that error go away but still...
using UnityEngine;
using System.Collections;
using System.Text;
//using http://System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using System.Runtime.Serialization;
using System.Reflection;
[Serializable ()]
public class SaveData : ISerializable {
public bool foundGem1 = false;
public float score = 42;
public int levelReached = 3;
public SaveData (SerializationInfo info, StreamingContext ctxt){
//Get the values from info and assign them to the appropriate properties
foundGem1 = (bool)info.GetValue("foundGem1", typeof(bool));
score = (float)info.GetValue("score", typeof(float));
levelReached = (int)info.GetValue("levelReached", typeof(int));
}
//Serialization function.
public void GetObjectData (SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("foundGem1", (foundGem1));
info.AddValue("score", score);
info.AddValue("levelReached", levelReached);
}
public void Save () {
SaveData data = new SaveData ();
Stream stream = File.Open("MySavedGame.game", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Binder = new VersionDeserializationBinder();
Debug.Log ("Writing Information");
bformatter.Serialize(stream, data);
stream.Close();
}
public void Load () {
SaveData data = new SaveData ();
Stream stream = File.Open("MySavedGame.gamed", FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Binder = new VersionDeserializationBinder();
Debug.Log ("Reading Data");
data = (SaveData)bformatter.Deserialize(stream);
stream.Close();
}
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;
}
}
}
and now I'm getting errors:
Assets/Scripts/Class/SaveData.cs(41,17): error CS0246: The type or namespace name `Stream' could not be found. Are you missing a using directive or an assembly reference?
and for the last class inside class I thought it would be wrong but it seems it's not as I get no errors so I'm even more confused as it seems I did steps correctly
I basically puted all in on bottom of each
I really don't understand 5% of this code
is it really necessary to write 3 pages just to save 1 int on HDD if not can someone show me simpler code how to save data on HDD so I could read write on it
Answer by Bunny83 · Feb 11, 2013 at 10:21 PM
Serialization is ment to provide a more or less type safe way to save arbitrary classes to a file and read them back in. If you have complex classes it can get very messy if you save things "manually" to a file. If the data you want to save isn't that complex you can simply use a BinaryWriter / Reader:
// C#
int someValue = 4;
void SaveData()
{
using(var writer = new BinaryWriter(File.OpenWrite("FileName")))
{
writer.Write(someValue);
writer.Close();
}
}
void LoadData()
{
using(var reader = new BinaryReader(File.OpenRead("FileName")))
{
someValue = reader.ReadInt32();
reader.Close();
}
}
You need of course the namespace System.IO
which contains any classes involved with file IO (input / output).
Just a little warning:
If you use this method to save binary data to a file it looses it's type. The binary data is just a stream of bytes. That's why it's important to read the exact same data that you have written. The Write method accepts a lot different types and it depends on the type you pass to the function what is actually written to the file.
The "normal" int-type is actually an int32. That means one int needs 32 bits (or 4 bytes) of memory. When you read the data back in you have to use the correct function which returns the exact type you've written.
Answer by Dave-Carlile · Feb 11, 2013 at 09:16 PM
If you just have a small amount of data to save, I would recomment using the built-in PlayerPrefs class.
and If I have large like lots of game objects
but I'd like to learn just 1 int just to get started?
Your answer
![](https://koobas.hobune.stream/wayback/20220613092527im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# line into Javascript help 1 Answer
Change level script 2 Answers
How to access a Component from another Child when current is Destroyed 1 Answer
adding a savable restrictions 0 Answers
Deserializing data with XML Serializer 0 Answers