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 BadAssGames · Oct 08, 2014 at 12:06 AM · serializationsaveloadrectbinaryformatter

Which types are actually serializable? Is the documentation incorrect or am I?

So I'm writing my own serializing method for a quick little app I'm making and have been running into some issues.

I have a data structure called SaveableData which stores a whole bunch of information that is important to resuming the state when the game is loaded. The SaveableData class is what gets serialized to the hard drive by my Binary Formatter.

 public class SaveLoad : MonoBehaviour 
 {
     public string saveName = "Tech_Tree_01";
     public string savePath = "/";
 
     public void SaveData(SaveableData dataToSave)
     {
         string path = Application.persistentDataPath + savePath + saveName + ".dat";
         FileStream userFile;
 
         if(!File.Exists(path))
         {//If the file isn't here, create it
             userFile = File.Create(path);
             userFile.Close();
         }
 
         //Then push all the data we need to save into this file
         BinaryFormatter binaryFormatter = new BinaryFormatter();
         userFile = File.Open(path, FileMode.Open);
         binaryFormatter.Serialize(userFile, dataToSave);//This function serializes all our data to file
         userFile.Close();
     }
 
 
     public void LoadData(string path)
     {
         SaveableData loadedData = new SaveableData();//This is the data that we're gonna load
         
         if(File.Exists(path))
         {
             BinaryFormatter binaryFormatter = new BinaryFormatter();
             FileStream userFile = File.Open(path, FileMode.Open);
             loadedData = (SaveableData)binaryFormatter.Deserialize(userFile);
             userFile.Close();
         }
         loadedData.LoadSaveableData();//After we have deserialized everything, load the data
     }


SaveableData contains data of the following types: ints, floats, strings, rects, colors, and vector2s. When attempting to serialize this data, I get the following errors:

SerializationException: Type UnityEngine.Rect is not marked as Serializable.

SerializationException: Type UnityEngine.Vector2 is not marked as Serializable.

SerializationException: Type UnityEngine.Color is not marked as Serializable.

http://docs.unity3d.com/ScriptReference/SerializeField.html

According to the documentation, the following is serializable:

All classes inheriting from UnityEngine.Object, for example GameObject, Component, MonoBehaviour, Texture2D, AnimationClip.. - All basic data types like int, string, float, bool. - Some built-in types like Vector2, Vector3, Vector4, Quaternion, Matrix4x4, Color, Rect, LayerMask.. - Arrays of a serializable type - List of a serializable type (new in Unity2.6) - Enums

NOTE: I have properly placed the [System.Serializable] Attribute at the top of the classes. My current version is 4.5 Pro.

I've looked all over and I can't find anyone else having these issues. Where am I going wrong? Is the documentation incorrect or am I making a mistake?

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 Cherno · Oct 08, 2014 at 12:28 AM 0
Share

I avoided this problem by creating workarounds for Unity-specific variables (3 floats for a Vector3 and so on), but while doing research for my own serialization script I got the impression that only if using Unity's built-in serializer can you serialize those classes, BUT the serialization system is primarily for non-runtime work (editor extensions and such).

This might just as well be just a whole bunch of baloney because I didn't dig too deep into the matter before deciding on workarounds, but it's how I understood the pieces of information I saw.

http://blogs.unity3d.com/2014/06/24/serialization-in-unity/

avatar image BadAssGames · Oct 08, 2014 at 12:37 AM 0
Share

I'm hoping this is not the case, as I would very much like to avoid doing that. It would make large data structures look VERY sloppy

avatar image devluz · Oct 08, 2014 at 12:41 AM 1
Share

The class BinaryFormatter is a pure c sharp system to serialize data but the link you are posting is from an internal unity serialization method. These two things don't work together. This is the correct documentation for your method: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter(v=vs.110).aspx

It can only use types that include the ISerializable interface e.g. the unity Rect class doesn't use this interface so you can't serialize it with this method. Sadly, you can't change unity classes so you have to serialize its values separately or using custom made structs/classes for this process

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by BadAssGames · Oct 08, 2014 at 05:56 AM

Well, Devluz pretty much answered this.

For anyone else having similar issues, the unfortunate solution I chose was to write a rather simple, but annoying function. This It looks something like this: (Pseudocode)

 System.Collection.Generic.List<float> floatList;
 
 public void SerializeData()
 {
     foreach(DataType data in myData)
     {
         //loop through and grab all the data as a float
         //then add that float to the float list
     }
 }
 
 public void DeserializeData()
 {
     foreach(float number in floatList)
     {
         //loop through and grab the floats and convert them 
         //back to their original data types *sigh*
     }
 }
 
 
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 Cherno · Oct 08, 2014 at 08:45 AM 0
Share

I sughgest looking into Unity Serializer, specifically the LevelSerializer.cs script to see how whole gameobjects components and all, can be serialized and deserialized.

avatar image
1

Answer by mikest · Dec 09, 2015 at 12:38 AM

If using the C# binary formatter, I think something like this is easier to implement and use than the float array. I have also used a similar technique to serialize Vector3's in Unity.

 [System.Serializable]
 public class SerializableColor {
     public float R;
     public float G;
     public float B;
     public float A;
     public SerializableColor (Color color) {
         R = color.r;
         G = color.g;
         B = color.b;
         A = color.a;
     }
     public Color GetColor () {
         return new Color(R, G, B, A);
     }
 }
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 Voxel-Busters · Aug 08, 2015 at 10:05 PM

Serialization of Unity objects are not directly possible using Binary Formatter. Check this post for more info.

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

32 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

Related Questions

Serialized data not getting saved in iPhone 1 Answer

Unity Serializer scripting error 1 Answer

Saving and Loading with a static or a local function 0 Answers

Load() not calling the the binaryformatter = file.create 0 Answers

Error serializing a class to save / load 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