- Home /
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 :)
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
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).
Thanks for the clarification. Edited my response :)
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.
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.
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.
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: Play mode:
Are you sure you're not clearing or re-declaring the Dictionary in any Start(), Awake() or Update() callback?
I've got only 1 reference to this scriptableobject with dictionary in entire project so that's impossible
Your answer
Follow this Question
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