I just can't get this to work. I want to serialize a class that holds a generic Dictionary which holds custom class objects. Here's a piece of code to show what I mean.
public class EditorData : ScriptableObject
{
[System.Serializable]
public class ObjectData
{
public SubObjectData[] listSubObjectData = null;
}
[System.Serializable]
public class SubObjectData
{
public System.Reflection.FieldInfo[] listFieldInfos = null;
public System.Object[] listValues = null;
}
public System.Collections.Generic.Dictionary<System.String, ObjectData> m_listObjectData = new System.Collections.Generic.Dictionary<System.String, ObjectData>( );
}
I tried serializing the dictionary of the EditorData object by myself using a BinaryFormatter but always got the error that the class System.Reflection.MonoField isn't marked as Serializable.
Then I tried saving the EditorData object as asset with AssetDatabase.CreateAsset, but when I restart Unity and load the asset, the dictionary is empty.
Any suggestions, hints, cake?
Unfortunattely Unity's built in serialization system doesn't support serializing System.Collections.Generic.Dictionary right now. As a workaround, you can store your data in two List<>'s one with keys and one with values, and "stich them together" in an Awake() function.
Check this post for more details: http://answers.unity3d.com/questions/1559/edit-and-continue-scripting-destroys-current-state/1583#1583