Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by StephenLTGAMES · May 11, 2011 at 10:22 AM · serializationsaveloadmenu

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Waz · Jul 15, 2011 at 11:54 PM 0
Share

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

avatar image volkan · Sep 18, 2012 at 02:22 PM 0
Share

this forum needs a SYNTAX HIGHLIGHTER/COLORING!

avatar image Rocketman1703 · Jul 29, 2017 at 08:17 PM 0
Share

I think i can help you, but what are you trying to do? Are you trying to make a button press script?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Coding help: How to use xml serialization 1 Answer

Easy way to save rigidbody state. 1 Answer

Error when loading saved date : SerializationException: End of Stream encountered before parsing was completed. 2 Answers

Implement save and load option in a game 2 Answers

Unknown Identifier: 'SerializeObject' 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges