- Home /
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
I tried to make it readable (because I've got a related issue), but perhaps you should repaste and mark it as code properly.
I think i can help you, but what are you trying to do? Are you trying to make a button press script?
Answer by ShadyProductions · Jul 29, 2017 at 09:18 PM
Probably the worst code i've ever put my eyes on.
 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;
Really? I mean.. Really??? Use an array for this lol.
 public bool[] ObjectivesComplete = new bool[10];
and then initialize like:
 for (int i=0; i < 10; i++) {
      ObjectivesComplete[i] = (bool)info.GetValue("Objective"+i+"Complete", typeof(bool));
 }
also the following is very bad to do:
 Stream stream = File.Open(filePath, FileMode.Create));
 BinaryFormatter bformatter = new BinaryFormatter();
 bformatter.Binder = new VersionDeserializationBinder();
 bformatter.Serialize(stream, data);
 stream.Close();
Better to enclose it inside a using statement so everything is disposed of correctly. Like so:
 using (Stream stream = File.Open(filePath, FileMode.Create))
 {
     BinaryFormatter bformatter = new BinaryFormatter();
     bformatter.Binder = new VersionDeserializationBinder();
     bformatter.Serialize(stream, data);
 }
I don't think we can help you much with such code tbh. If its written like that, its too sloppy too much of a mess.
Your answer
 
 
             Follow this Question
Related Questions
Coding help: How to use xml serialization 1 Answer
Easy way to save rigidbody state. 1 Answer
Implement save and load option in a game 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                