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 /
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.

avatar image PizzaPie Hellium · Oct 07, 2018 at 12:52 PM 0
Share

That statement from Unity though, pure , they could just admit their serializer is badly written and can't be expanded at this point, dictionaries and graphs needed pretty much on all games.

As for the original question, don't mix up the Serializers, Unity's Serializer != c# serializer, what is Serializable via one may not be serializable via the other, they both make use of the [Serializable] attribute but that's all they have in common.

avatar image Khena_B PizzaPie · Oct 07, 2018 at 04:33 PM 0
Share

I did mix up the serializers, I don't really code outside of Unity so I tend to associate VS and coding with Unity, when I heard that Unity couldn't serialize Dictionaries I didn't understand why i was able to do so using the BinaryFormatter but now I get it.

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.

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 List>??? 0 Answers

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

Serialize loads of different data 0 Answers

How to save sprite color change on custom inspector ? 0 Answers

Got a Problem when serializing Class Dictionary with Custom Editor 1 Answer


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