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 /
This question was closed Jan 06, 2014 at 11:04 PM by AeonIxion for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by AeonIxion · Jan 06, 2014 at 12:05 PM · saveload

Saving and loading data from file

Hey,

I'm trying to save and load to/from a file. I've looked at this question and am trying to use it in my project to save the character's position. I'm really not sure how this works though..

     public static void Save(string filePath)
         {
             SaveNLoadSerialize data = new SaveNLoadSerialize();
             Stream stream = File.Open(filePath, FileMode.OpenOrCreate);
             BinaryFormatter bf = new BinaryFormatter();
             bf.Binder = new VersionDeserializationBinder();
             bf.Serialize(stream, data);
             stream.Close();
         }
 

In that piece of code, how does it know what data it should save?

Also..how do I access the save method from another script? I've tried making an object of the SaveNLoad class and call the save method, but not sure if that's the right way to do it.

Below is my full code:

 using UnityEngine;
 
 using System.Collections;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Runtime.Serialization;
 using System.Reflection;
 using System.IO;
 using System;
 using System.Text;
 
 [Serializable()]
 public class SaveNLoadSerialize : ISerializable {
         
     public GameObject character;
     public SaveNLoad snl = new SaveNLoad();
 
     public float x;
     public float y;
     public float z;
     
 
     public SaveNLoadSerialize()
     {
         
     }
 
     public SaveNLoadSerialize(SerializationInfo info, StreamingContext ctx)
     {
         x = (float)info.GetValue("x", typeof(float));
         y = (float)info.GetValue("y", typeof(float));
         z = (float)info.GetValue("z", typeof(float));
     }
 
     public void GetObjectData(SerializationInfo info, StreamingContext ctx)
     {
         info.AddValue("x", x);
         info.AddValue("y", y);
         info.AddValue("z", z);
     }
 
     public class SaveNLoad
     {
         public static string currentFilePath = "SaveNLoad.miko";
         public  void Save()
         {
             Debug.Log("saving");
             Save (currentFilePath);
 
         }
 
         public static void Save(string filePath)
         {
             SaveNLoadSerialize data = new SaveNLoadSerialize();
             Stream stream = File.Open(filePath, FileMode.OpenOrCreate);
             BinaryFormatter bf = new BinaryFormatter();
             bf.Binder = new VersionDeserializationBinder();
             bf.Serialize(stream, data);
             stream.Close();
         }
 
 
         public void Load()
         {
             Debug.Log("Loading");
             Load (currentFilePath);
 
         }
 
         public static void Load(string filePath)
         {
             SaveNLoadSerialize data = new SaveNLoadSerialize();
             Stream stream = File.Open(filePath, FileMode.Open);
             BinaryFormatter bf = new BinaryFormatter();
             bf.Binder = new VersionDeserializationBinder();
             data = (SaveNLoadSerialize)bf.Deserialize(stream);
             stream.Close();
 
     
             Debug.Log(data.x);
         }
 
     }
 
 
     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; 
         } 
     }


I hope someone can help me with this.

Comment
Add comment · Show 1
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 AeonIxion · Jan 06, 2014 at 11:04 PM 0
Share

nvm im stupid. need to add data.x = character.transform.position.x to the save method

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Find SceneIndex out C# 1 Answer

Serialization/Deserialization Problems 0 Answers

,Block Levels and save progress 1 Answer

How can I continue the game without losing values after Game Over? 1 Answer

save load exp on database 2 Answers


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