Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by DeathOfAshes · Jun 06, 2017 at 01:53 AM · save datasaveload

Save and Load Using BinaryFormatter HELP!!!

So I am trying to make a save and load system using BinaryFormatter/Custom Binary and I am trying to figure out how to save a float and a bool using it, so far I have made a class SaveManager which has all the save and load functions, and then I have script which access the static function in the SaveManger class. Here the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public static class SaveManager {
     public static string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
 
     public static void SavePlayer(SavePos player)
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream stream = new FileStream(path + "/PlayerPos.txt", FileMode.Create);
 
         PlayerData data = new PlayerData(player);
 
         bf.Serialize(stream, data);
         stream.Close();
     }
 
     public static float[] LoadPlayer()
     {
         
         if (File.Exists(path + "/PlayerPos.txt"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream stream = new FileStream(path + "/PlayerPos.txt", FileMode.Open);
 
             PlayerData data = bf.Deserialize(stream) as PlayerData;
             stream.Close();
 
             return data.stats;
         }
         return null;
     }
 }
 
 [Serializable]
 public class PlayerData
 {
     public float[] stats;
 
     public PlayerData(SavePos player)
     {
         stats = new float[3];
         stats[0] = SavePos.posX;
         stats[1] = SavePos.posY;
         stats[2] = SavePos.posZ;
     }
 }

The Class I am accessing the SaveManager From:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 
 public class SavePos : MonoBehaviour {
     public static float posX;
     public static float posY;
     public static float posZ;
 
     public void save()
     {
         float xx = transform.position.x;
         float yy = transform.position.y;
         float zz = transform.position.z;
     }
 
     public void load()
     {
         float[] loadedStats = SaveManager.LoadPlayer();
 
         float x = loadedStats[0];
         float y = loadedStats[1];
         float z = loadedStats[2];
 
         transform.position = new Vector3(x,y,z);
     }
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by agray427 · Jan 15, 2018 at 08:57 PM

I'm not sure why you have all the references set up the way that you do, it seems strange, but this should work fine, though I haven't tested it (but I will if I must):

 using System;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using UnityEngine;
 
 public static class SaveManager
 {
     private static readonly string FILE_PATH = Path.Combine(Application.persistentDataPath, "example.txt");
 
     private static PlayerData m_playerData;
 
     public static void Save()
     {
         BinaryFormatter bf = new BinaryFormatter();
         SavePlayer(bf);
     }
 
     private static void SavePlayer (BinaryFormatter bf)
     {
         if (m_playerData != null)
         {
             using (FileStream stream = new FileStream(FILE_PATH, FileMode.Open, FileAccess.Read))
             {
                 bf.Serialize(stream, m_playerData);
             }
         }
     }
 
     public static void Load ()
     {
         BinaryFormatter bf = new BinaryFormatter();
 
         if (!LoadPlayer(bf))
         {
             m_playerData = new PlayerData();
         }
     }
 
     private static bool LoadPlayer (BinaryFormatter bf)
     {
         if (!File.Exists(FILE_PATH))
         {
             return false;
         }
 
         using (FileStream stream = new FileStream(FILE_PATH, FileMode.Open, FileAccess.Read))
         {
             m_playerData = bf.Deserialize(stream) as PlayerData;
         }
 
         return true;
     }
 
     [Serializable]
     public class PlayerData
     {
         [SerializeField]
         private Pos3D m_position;
     }
 
     [Serializable]
     public struct Pos3D
     {
         public static readonly Pos3D zero = new Pos3D(0.0f, 0.0f, 0.0f);
         public static readonly Pos3D one = new Pos3D(1.0f, 1.0f, 1.0f);
 
         [SerializeField]
         private float m_x;
         [SerializeField]
         private float m_y;
         [SerializeField]
         private float m_z;
 
         public float x { get { return m_x; } set { m_x = value; } }
         public float y { get { return m_y; } set { m_y = value; } }
         public float z { get { return m_z; } set { m_z = value; } }
 
         public Pos3D (float x, float y, float z)
         {
             m_x = x;
             m_y = y;
             m_z = z;
         }
     }
 }
Comment
Add comment · Show 1 · 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
avatar image agray427 · Jan 15, 2018 at 08:59 PM 0
Share

Quick side note, I created it this way because it is a Save$$anonymous$$anager. I assumed that there would be more than just the player being created. In that case, you will want more of the specific private methods like SavePlayer and LoadPlayer. Likewise, you will also want to have different file paths. Best of luck!

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

107 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 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 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 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 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 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

[Error:] Cannot Deserialize JSON to new instances of type ' X ' 1 Answer

How do i Make this saving system apply the save data in my game. 0 Answers

How to SAVE/LOAD Unity Game in Mac/WebGL? 0 Answers

how to save a lot of character data? 1 Answer

Vroid Opening Incorrect Save,Vroid opens the model above the one I click 0 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