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 Ochreous · Dec 13, 2012 at 06:17 PM · c#serializationbinary

C# how to setup a Binary Serialization

Hi everyone, I need help setting up a binary Serialization in unity. I have no clue how to set one up but I would like to so I can save my values to a File. I don't want to use Playerprefs because I want to have total control over my save files.

     using UnityEngine;
     using System.Collections;
      
     public class ExampleClass : MonoBehaviour{
     public int ExampleInt;
     public bool ExampleBool;
     public float ExampleFloat;
     public string ExampleString;
     public int[] ExampleArray;
     public List<string> ExampleList  = new List<string>();
     //What comes next?
     }
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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by skalev · Dec 14, 2012 at 02:25 AM

Edit :

So, in your serialization class, add this function:

 // === This is required to guarantee a fixed serialization assembly name, which Unity likes to randomize on each compile
 // 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;
     }
 }


The next phase is to actually serialize. In order to serialize a class it needs to be tagged with [System.Serializable].

Now, the actual serialization process is pretty simple:

 public static void Save(MyClassIWantToSerialize class, string filePath)
     {
         try
         {
             Stream stream = File.Open(filePath, FileMode.Create);
             BinaryFormatter bformatter = new BinaryFormatter();
             bformatter.Binder = new VersionDeserializationBinder();
             bformatter.Serialize(stream, class);
             stream.Close();
         }
         catch (UnauthorizedAccessException) //many more exception might happen, check documentation
         {}
 }
Comment
Add comment · Show 9 · 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 Ochreous · Dec 14, 2012 at 02:32 AM 0
Share

I found that a while ago before I started this question. It didn't help me with my setup.

avatar image skalev · Dec 14, 2012 at 02:36 AM 0
Share

Ok. well that is how you do it. If you can shed more light on WHY this isn't helpful, we can try and help you further.

avatar image Ochreous · Dec 14, 2012 at 02:57 AM 0
Share

The reason why it isn't helpful is I don't understand what is going on within the answer. Its bits and pieces of binary serialization and not a full script and since it is not a full script its hard for me to figure out how it all works.

avatar image skalev · Dec 14, 2012 at 03:06 AM 0
Share

fair enough. Answer edited.

also, i recommend reading this :

http://blogs.unity3d.com/2012/10/25/unity-serialization/

alot of good information, even if you aren't going to use it.

avatar image Ochreous · Dec 14, 2012 at 03:19 AM 0
Share

I'm sorry I'm still not catching on. What I need is a full script so I can understand how it works.your new post is still bits and pieces.

Show more comments
avatar image
0

Answer by jogo13 · Dec 13, 2012 at 11:02 PM

It's in C# but this may help: http://pixelplacement.com/2012/03/21/simple-state-saving/

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 Ochreous · Dec 14, 2012 at 12:47 AM 0
Share

I don't want to save the state of an object. I just want to save the values of my variables with Binary serialization.

avatar image
0

Answer by MidnightStudiosInc · Jul 01, 2015 at 03:41 PM

I would recommend using GameObject Serializer Pro over BinaryFormatter because:

  1. It's built on top of protobuf-net, which is very efficient and unlike BinaryFormatter is forward-compatable with future versions of .Net/Mono

  2. There is built-in support for serializing many primitive Unity types like Vector3 and Quaternion

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
avatar image
0

Answer by herbou · Jun 24, 2020 at 11:34 AM

A Generic BinarySerializer tool , Easy to use. Supports : Vectors, Colors, and Quaternions too.

https://youtu.be/PbPCW8vK3RQ

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

14 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

Related Questions

Serializing strings to XML C# 1 Answer

Player lives script help 1 Answer

C# -- Build character unit from script 1 Answer

C# How to Drag and Scale with Mouse Window 0 Answers

Best practice to store and load a specific set of objects? 3 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