Question by
LRP · Jan 20, 2019 at 10:03 AM ·
listserializationgeneric
Serializing a List of generic classes
Hello,
I am trying to create a look up table for ScriptableObjects. I am unable to have Unity serialize a list of generic classes though. Any ideas on how I can fix that or how I can make my look up table differently ?
Here is my class:
public class LookUpTable<T> : ScriptableObject where T : ScriptableObject
{
[SerializeField]
private List<TableEntry<T>> Elements = new List<TableEntry<T>>();
public int GetKey(T value)
{
for (int i = 0, j = Elements.Count; i < j; i++)
{
if (Elements[i].Value == value) return Elements[i].Key;
}
return -1;
}
public T GetValue(int key)
{
for (int i = 0, j = Elements.Count; i < j; i++)
{
if (Elements[i].Key == key) return Elements[i].Value;
}
return default(T);
}
}
[Serializable]
public class TableEntry<T>
{
public int Key;
public T Value;
}
Thank you
Comment