vexe / VFW

MIT License
492 stars 67 forks source link

Dictionary values set in inspector fail to persist across scene change #96

Closed BlacKCaT27 closed 8 years ago

BlacKCaT27 commented 8 years ago

Not sure if I'm doing something incorrectly or if there's an issue with the library, but I'm unable to get the values I set in the inspector to persist. Once I run the application or transition to another scene in the Editor, the values are gone.

Here's some more info of the configurations I've tried:

[SerializeField]
private SerializableDictionary<MyEnum, Texture> _headlightTextureMap; 

[SerializeField]
private Dictionary<MyEnum, Texture> _headlightTextureMap; 

[SerializeField]
private Dictionary<MyEnum, Texture> _headlightTextureMap = new Dictionary<MyEnum, Texture>(); 

[SerializeField]
private SerializableDictionary<MyEnum, Texture> _headlightTextureMap = new SerializableDictionary<MyEnum, Texture>(); 

public SerializableDictionary<MyEnum, Texture> _headlightTextureMap = new SerializableDictionary<MyEnum, Texture>(); 

In all cases, the dictionary appears in the inspector and I can add/remove/clear values, but when I save the scene and move to a different scene, the values are gone when I come back, and the dictionary is empty at runtime.

Am I missing something? Or is there something I'm doing that's unsupported? I tried using [Serialize] instead of [SerializeField], but the class wasn't found. Is the documentation on that out of date?

BlacKCaT27 commented 8 years ago

Update: I was able to resolve this issue thanks to an answer found here:

http://answers.unity3d.com/questions/460727/how-to-serialize-dictionary-with-unity-serializati.html

Turns out, Unity's serializer has issues with generics (like SerializableDictionary), but you can avoid the issue by subclassing the generic with concrete type values that you want to use. So I created a new class:

[Serializable]
MyEnumTextureMap : SerializableDictionary<MyEnum, Texture>{}

And the data now persists properly.

Leaving this note here in the hopes that the developer can use this info to make the inspector functionality work better somehow.