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