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 deathripper · Oct 03, 2020 at 11:12 AM · saveload

Binary Save Class Array

Hey guys,

I am quite new to programming and especially saving, so I watched a couple of Tutorials and tried to rebuild a binary save / load system which seems to work fine for some variables, however an arrasy containing variables from another class does not seem to get saved. Would appriciate if anyone could tell me whihc mistake I made. Here is my Save/Load script:

 using UnityEngine;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 
 public class SerializationManager
 {
     public static bool Save(string saveName, object saveData)
     {
         BinaryFormatter formatter = GetBinaryFormatter();
        
         if (!Directory.Exists(Application.persistentDataPath + "/TapJoy"))
         {
             Directory.CreateDirectory(Application.persistentDataPath + "/TapJoy");
         }
 
         string path = Application.persistentDataPath + "/TapJoy/" + saveName + ".save";
         
         FileStream file = File.Create(path);
 
         formatter.Serialize(file, saveData);
         file.Close();
 
         return true;
     }
 
     public static object Load(string path)
     {
         if (!File.Exists(path))
         {
             return null;
         }
 
         BinaryFormatter formatter = GetBinaryFormatter();
         FileStream file = File.Open(path, FileMode.Open);
 
         try
         {
             object save = formatter.Deserialize(file);
             file.Close();
             return save;
         }
         catch
         {
             Debug.LogErrorFormat("Failed to load file at {0}", path);
             file.Close();
             return null;
         }
     }
 
     public static BinaryFormatter GetBinaryFormatter()
     {
         BinaryFormatter formatter = new BinaryFormatter();
         return formatter;
     } 
 }
 

And then:

 using System.Collections;
 using UnityEngine;
 
 [System.Serializable]
 public class SaveData
 {
     private static SaveData _current;
     public static SaveData current
     {
         get
         {
             if (_current == null)
             {
                 _current = new SaveData();
             }
             return _current;
         }
         set
         {
             if (value != null)
             {
                 _current = value;
             }
         }
     }
     public PageValues pageValues = new PageValues();
 }
 

And Finally my custom classes:

 using System;
 using UnityEngine;
 
 [System.Serializable]
 public class PageValues
 {
     public Tiles[] tiles;
 
     public float timer;
     public int starRating;
     public int failCounter;
 
     [Serializable]
     public class Tiles
     {
         public bool isColored = false;
         public Color color;
     }
 }
 
 

Somehow the Tiles[] tiles; array seems not to get saved.

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 Bunny83 · Oct 03, 2020 at 03:46 PM

Unity's Color struct is not marked with the Serializable attribute and therefore is not serialize by the binaryformatter. You could implement a serialization surrogate for the Color struct or use a seperate serializable struct to hold the color information


Anyways, I would not recommend using the BinaryFormatter. It has several issues, specifically:

  • The binary format is well known and doesn't provide much security / protection against manipulations. Security is often one of the main reasons by people seem to choose the binary formatter.

  • The format is quite verbose which makes it easy to actually reconstruct your data classes from a save file without seeing or decompiling your code.

  • Due to the tight coupling with your data classes you can easily run into versioning issues whenever you want to add, remove or change any field in your data structure. This is mainly an issue when you update your application and users already have an old save file.

  • Since the format is that verbose in the average case it's not really smaller than json and in some edge cases can be even larger (never try to save multidimensional arrays or generic types with it).

  • The binary nature of the save file makes it difficult to debug any issues you may have.

  • The binary remoting format that is used by he binary formatter also makes your application vulnerable to deserialization attacks because one can embed any class inside the binary stream which is happily deserialized by the binary formatter. Microsoft even has a security warning against the use of the BinaryFormatter

That's why in many cases it's better to use json or another human readable format. To make manipulations more difficult you can always add some obfuscation at the end. The BinaryFormatter was never meant for saving / loading information to / from files. It's just part of the remoting protocol for remote procedure calls.


Though the binary formatter has one feature that most serializers usually do not support which is polymorphism. However you rarely would need something like that. Anyways it's up to you what you're using in the end.

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

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

Problem to add a delete method to a saveload script C# 3 Answers

How to save custom data types 1 Answer

save / load xml file on a web server 1 Answer

How to save list of Sprites? 1 Answer

How to check is google play account opening game for the first time 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