- Home /
please help me, Scriptable object's variable serialization issue
I have a dictionary stored in a ScriptableObject, which then is saved as an asset.
Dictionary<Transform, Nodescript> ScriptViaTransform = new Dictionary<Transform, Nodescript>();
Once I load the asset, my editor classes can pass values for storage, but if I Enter/Exit playmode, it looks like the dictionary re-initializes itself. This wipes all the keys/values :<
Question, how to store the dictionary data in a Scriptable object so that its dictionary doesn't re-initialize itself upon the initialization? And how do I initialize dictionary only once?
Thank you
Answer by Habitablaba · Oct 30, 2014 at 04:29 AM
Since you added the 'serialization' tag, I'm assuming you know your problem is with saving a dictionary to a file, and you're not looking for some in-code magic to persist a variable across runtimes.
Howerver, a dictionary is not serialize-able by default, so you cannot save it to a file and reload its state. There are a bunch of answers and forum posts and tutorials (and assets on the store) which provide the concept of a serialize-able dictionary, so I would start there. Other than that, you can always change data structures, or attempt to write your own version of a serialize-able dictionary.
This, for example, seems like a good place to start.
This is bad news :< Is there a way to dump it all in an array and save it in the other scriptable object?
http://stackoverflow.com/questions/14495476/how-to-add-dictionary-data-in-to-an-array got it, thanks for your time!
That's for objective C, but I suppose the concept is sound.
Thanks Habitablaba, I've found something that looks very promising and is basically what the other question you've directed me to implements http://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.OnBeforeSerialize.html for anybody struggling.
Thank you m8!