Serialize a List of generic key value pair in a ScriptableObject

Hi,

I am trying to use ScriptableObjects as databases by storing lists of indexed values.
As dictionary are not natively serializable I tried using lists like so

    [System.Serializable]
    public class TableEntry<T>
    {
        public string key;
        public T value;
    }
    
    public abstract class DataTable<T> : ScriptableObject
    {
        public List<TableEntry<T>> entries;
    }

    [CreateAssetMenu(menuName = "ObjectTable")]
    public class ObjectTable : DataTable<object>
    {
        public string GetKey(object value)
        {
            for (int i = 0, j = entries.Count; i < j; i++)
            {
                if (entries*.value == value)*

{
return entries*.key;*
}
}
return null;
}

public object GetValue(string key)
{
for (int i = 0, j = entries.Count; i < j; i++)
{
if (entries*.key == key)*
{
return entries*.value;*
}
}
return null;
}
}

But whatever I do Unity seems to refuse serializing containers of generic types.
I tried different solutions but I always end up breaking the genericity or with no serialization at all.
Is there a known workaround for this ?
Thanks

Unity’s serializer does not support generic types. The serialization happens on the native C++ side. Every type has to be non generic in order to be serialized. Further more the “object” types (System.Object) is not supported as well since the serialization happens based on values, not references. It also doesn’t support inheritance / polymorphism for custom serializable classes (since the fields are serialized by values based on the field type).

If you need inheritance, you have to use either ScriptableObjects or MonoBehaviours as they are the only classes which support inheritance. Though those classes need to be serialized seperately.

See the manual for more information