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
0
Question by Skullek · Oct 11, 2019 at 10:19 AM · editor-scriptingserializationdictionaryvaluescollections

Saving dictionary from editor mode in play mode

I'm creating a lot of things via inspector script in editor mode. This script has a reference to a dictionary in ScriptableObject which has some elements (so i can see that script add values to dictionary in editor mode) but when i'm entering play mode the dictionary is empty... I tried to use [SerializeField] and [OdinSerialize] but it doesnt' work.

How can I save dictionary values from editor mode in play mode?

I don't want to edit values in inspector. My script populates these values in edit mode.I call the function and have 19 entries in dictionary. When i press play i've got an empty dict.

Thanks :)

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 Em3rgency · Oct 11, 2019 at 12:04 PM

Dictionaries are not serializable. If you want to edit dictionary values in the inspector, you will need to write a custom serializer for your dictionaries.

Here is an asset that does it for you: https://assetstore.unity.com/packages/tools/integration/serializabledictionary-90477

Here is a github repo of someones implementation: https://github.com/azixMcAze/Unity-SerializableDictionary

Comment
Add comment · Show 10 · 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 Bonfire-Boy · Oct 11, 2019 at 12:09 PM 1
Share

While the solution given is a good one, you're characterising it wrongly. The issue isn't the lack of a property drawer, it's the fact that the standard Dictionary class isn't Serializable.

If it were, their values would persist (assu$$anonymous$$g they marked things dirty and so on) regards of whether there were a property drawer for it (that just makes it possible to edit it the values in the inspector).

avatar image Em3rgency Bonfire-Boy · Oct 11, 2019 at 01:01 PM 0
Share

Thanks for the clarification. Edited my response :)

avatar image Skullek · Oct 11, 2019 at 12:30 PM 0
Share

I don't want to edit values in inspector. $$anonymous$$y script populates these values in edit mode.I call the function and have 19 entries in dictionary. When i press play i've got an empty dict.

avatar image Em3rgency Skullek · Oct 11, 2019 at 12:59 PM 0
Share

Do you call new Dictionary() in Start or Awake or whatever? That would clear your values when starting play. I have no idea why else it would happen, as I have populated dictionaries and used them in play many times successfully.

avatar image Bonfire-Boy Skullek · Oct 11, 2019 at 01:06 PM 0
Share

I think that using a serializable dictionary as Em3rgency suggests should fix this. Being able to also edit/view the saved values in the inspector would be a bonus.

Another approach would be to create your own pseudo-dictionary ie define a simple Serializable class to hold the key-value pairs, and then have an array of such pairs as a field ins$$anonymous$$d of the dictionary. But of course you'd need to write a bunch of access methods too, which the things linked to in the answer would give you for free.

avatar image Skullek · Oct 11, 2019 at 01:58 PM 0
Share

I used it and now I can see dictionary in inspector and i see that in editor everything works fine and it has got every variable.

But when I press play it sets up to 0 again.

Editor: alt text Play mode: alt text

avatar image Em3rgency Skullek · Oct 11, 2019 at 02:26 PM 0
Share

Are you sure you're not clearing or re-declaring the Dictionary in any Start(), Awake() or Update() callback?

avatar image Skullek Em3rgency · Oct 11, 2019 at 02:34 PM 0
Share

I've got only 1 reference to this scriptableobject with dictionary in entire project so that's impossible alt text

alt text

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

129 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

Related Questions

Help with editor serialization 1 Answer

Editor Script - Multi-Editing Value Increase/Decrease Issue 1 Answer

Serializable settings for custom PropertyDrawer 1 Answer

Serialize a Dictionary 1 Answer

Dynamic serialized fields based on enum 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