A package for Unity to serialize Dictionary, HashSet and KeyValuePair.
This project is developed from azixMcAze's SerializableDictionary.
Go to "MenuBar > Window > Package Manager > Add > Add package from git URL" and enter the URL "https://github.com/StromKuo/SerializableDictionary.git"
Download and extract this project and put it in the Packages folder of your project.
Not yet...
Before Unity 2020.1, Unity doesn't support serialization of generic class, you need to create a derived class from SerializableDictionary
, SerializableHashSet
or SerializableKeyValuePair
.
[SerializeField]
StringStringDictionary m_StringStringDictionary;
[SerializeField]
StringMyClassDictionary m_StringMyClassDictionary;
[SerializeField]
ColorHashSet m_ColorHashSet;
[SerializeField]
StringIntPair m_StringIntPair;
[Serializable]
public class StringStringDictionary : SerializableDictionary<string, string> {}
[Serializable]
public class MyClass
{
public int i;
public string str;
}
[Serializable]
public class StringMyClassDictionary : SerializableDictionary<string, MyClass> {}
[Serializable]
public class ColorHashSet : SerializableHashSet<Color> {}
[Serializable]
public class StringIntPair : SerializableKeyValuePair<string, int> {}
After Unity 2020.1:
[SerializeField]
SerializableDictionary<string, string> m_StringStringDictionary;
[SerializeField]
SerializableDictionary<string, MyClass> m_StringMyClassDictionary;
[SerializeField]
SerializableHashSet<Color> m_ColorHashSet;
[SerializeField]
SerializableKeyValuePair<string, int> m_StringIntPair;
[Serializable]
public class MyClass
{
public int i;
public string str;
}
Unity cannot serialize a array of lists or an array of arrays.
Create a class that inherits from SerializableDictionaryStorage<List<TValue>
. This storage class will only contain a List<TValue> data
field.
[SerializeField]
StringColorListDictionary m_colorStringListDict;
[Serializable]
public class ColorListStorage : SerializableDictionaryStorage<List<Color>> {}
[Serializable]
public class StringColorListDictionary : SerializableDictionary<string, ColorListStorage> {}
// you would have to access the color list through the .data field of ColorListStorage
List<Color> colorList = m_colorStringListDict[key].data;