x


Saving/Load using menu C#

I found this code and have tried to edit it implement the code to work on button press.

using UnityEngine; // For Debug.Log, etc.

using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Binary;

using System; using System.Runtime.Serialization; using System.Reflection; using System.Collections;

// === This is the info container class ===

[Serializable ()]

public class SaveData : ISerializable {

public bool Objective1Complete = false; public bool Objective2Complete = false; public bool Objective3Complete = false; public bool Objective4Complete = false; public bool Objective5Complete = false; public bool Objective6Complete = false; public bool Objective7Complete = false; public bool Objective8Complete = false; public bool Objective9Complete = false; public bool Objective10Complete = false;
public int exp ; public int levelReached;

public SaveData () { } public SaveData (SerializationInfo info, StreamingContext ctxt) {

Objective1Complete = (bool)info.GetValue("Objective1Complete", typeof(bool)); Objective2Complete = (bool)info.GetValue("Objective2Complete", typeof(bool)); Objective3Complete = (bool)info.GetValue("Objective3Complete", typeof(bool)); Objective4Complete = (bool)info.GetValue("Objective4Complete", typeof(bool)); Objective5Complete = (bool)info.GetValue("Objective5Complete", typeof(bool)); Objective6Complete = (bool)info.GetValue("Objective6Complete", typeof(bool));

Objective7Complete = (bool)info.GetValue("Objective7Complete", typeof(bool)); Objective8Complete = (bool)info.GetValue("Objective8Complete", typeof(bool)); Objective9Complete = (bool)info.GetValue("Objective9Complete", typeof(bool)); Objective10Complete = (bool)info.GetValue("Objective10Complete", typeof(bool));

exp = (int)info.GetValue("Exp", typeof(int)); levelReached = (int)info.GetValue("levelReached", typeof(int)); }

// Required by the ISerializable class to be properly serialized. This is called automatically

public void GetObjectData (SerializationInfo info, StreamingContext ctxt) { // Repeat this for each var defined in the Values section info.AddValue("Objective1Complete", (Objective1Complete)); info.AddValue("Objective2Complete", (Objective2Complete)); info.AddValue("Objective3Complete", (Objective3Complete)); info.AddValue("Objective4Complete", (Objective4Complete)); info.AddValue("Objective5Complete", (Objective5Complete)); info.AddValue("Objective6Complete", (Objective6Complete)); info.AddValue("Objective7Complete", (Objective7Complete)); info.AddValue("Objective8Complete", (Objective8Complete)); info.AddValue("Objective9Complete", (Objective9Complete)); info.AddValue("Objective10Complete", (Objective10Complete));

info.AddValue("Exp", exp);
info.AddValue("levelReached", levelReached);
} }

// === This is the class that will be accessed from scripts === public class SaveLoad : MonoBehaviour{

public static string currentFilePath = "SaveData.cjc";

// Call this to write data public static void Save ()
{ bool SavingProcess = PauseMenu.SaveProgress;

  if(SavingProcess == true)
  {
      Debug.Log("SavingSomething");
Save (currentFilePath);
  }
} public static void Save (string filePath) { Debug.Log("Saving"); SaveData data = new SaveData ();

Stream stream = File.Open(filePath, FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Binder = new VersionDeserializationBinder(); 
bformatter.Serialize(stream, data);
stream.Close();
}

public static void Load () {

  bool LoadingProcess = PauseMenu.LoadProgress;

  if(LoadingProcess == true)

  {
  Debug.Log("LoadingSomething");
  Load(currentFilePath);  }     }
public static void Load (string filePath) { SaveData data = new SaveData (); Stream stream = File.Open(filePath, FileMode.Open); BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Binder = new VersionDeserializationBinder(); data = (SaveData)bformatter.Deserialize(stream); stream.Close();

// Now use "data" to access your Values
}

}

// Do not change this
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; 
}

Any thoughts on what i am doing wrong

more ▼

asked May 11, 2011 at 10:22 AM

StephenLTGAMES gravatar image

StephenLTGAMES
56 9 6 8

I tried to make it readable (because I've got a related issue), but perhaps you should repaste and mark it as code properly.

Jul 15, 2011 at 11:54 PM Waz

this forum needs a SYNTAX HIGHLIGHTER/COLORING!

Sep 18, 2012 at 02:22 PM volkan
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x658
x644
x599
x404

asked: May 11, 2011 at 10:22 AM

Seen: 3031 times

Last Updated: Sep 18, 2012 at 02:22 PM