• 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
1
Question by Khena_B · Oct 06, 2018 at 10:20 PM · serializationdictionaryserializebinaryformattertypeof

Serialize a Dictionary

According to the docs a Dictionary can't be serialized in Unity so I was thinking about buying an asset that can do that so that i can save my dictionaries to file, but I'm able to serialize and deserialize a Dictionary using the BinaryFormatter, I'm not sure I understand, can someone explain the difference between serialization and what the BinaryFormatter does?

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 Hellium · Oct 06, 2018 at 10:27 PM 0
Share

I don't know where you have seen that Dictionary can't be serialized. They are serializable, Unity simply does not do it.

Serializers in Unity run in a real-time game environment. This has a significant impact on performance. As such, serialization in Unity behaves differently to serialization in other program$$anonymous$$g environments

https://docs.unity3d.com/$$anonymous$$anual/script-Serialization.html

If you want to have a custom serialization system, you are free to do so.

Show more comments

1 Reply

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

Answer by Prastiwar · Oct 07, 2018 at 06:36 AM

Dictionaries can be serialize, but unity does not do it.
There is even example in docs how to do it: https://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.html
There are some assets (free or not) to also draw it in inspector, one of them you can see there:
https://github.com/Prastiwar/UnitySerializedDictionary

Comment
Add comment · Show 4 · 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 Khena_B · Oct 07, 2018 at 06:45 AM 0
Share

Can you answer this, why am I able to save (serialize?) Dictionaries using the BinaryFormatter, isn't this serialization? This is what I'm trying to understand, I don't need to serialize Dictionaries to see them in the inspector, my goal is to save them to file using the BinaryFormatter and it seems to work, I'm trying to understand why and if this is something I should do or not before I start doing it a lot.

avatar image Prastiwar Khena_B · Oct 07, 2018 at 08:31 AM 0
Share

Yes, process of saving and loadind is simply called serialization.
There is nothing wrong to save data with BinaryFormatter (also json, xml, prefs and others), they exists to do exactly that.
Why you can do that? Because Dictionary is serializable - that means you can save and load it.
You need to remember a key to serialize(save/load) data is [Serializable] attribute on class/struct that allows it to be serialized, in unity also [SerializeField] on fields, primitives are serializable by default.

Some rules to use serialization in Unity you can find THERE.
Also docs from $$anonymous$$SDN is also worth to read.

avatar image Glurth Prastiwar · Oct 07, 2018 at 06:42 PM 0
Share

"There is nothing wrong to save data with BinaryFormatter ..."

What about object references? I'm not really sure how unity serializes these, but if the project contents change sufficiently, couldn't references stored via BinaryFormatter, end up pointing at the wrong object?

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

99 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

Related Questions

Serialize loads of different data 0 Answers

Serialize List>??? 0 Answers

BinaryFormatter - saving and loading a list containing sprites. 0 Answers

EndOfStreamException : Failed to read past end of stream 1 Answer

In this case the lists do not serialize. Why? 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