I’m making an inventory system for my game, and I was wondering how I could serialize the InventoryItem class
using System.Collections.Generic;
class InventoryItem
{
List<IAttribute> Attributes = new List<IAttribute>();
}
//This interface is used so Attributes of all data types
//Can be used in one list
interface IAttribute
{
}
class Attribute<T> : IAttribute
{
T value;
public Attribute<T>(T Value)
{
value = Value;
}
public void SetValue(T Value)
{
value = Value;
}
}
You can serialize a generic class hierarchy if they all share one base class that is not generic. You can inherit a generic class from the base class, then inherit all other implementations from the generic class in the middle of the hierarchy. Unity needs the non-generic base class for serialization to work. It also needs all of the final classes to be closed, concrete implementations of the generic class. It doesn’t support serializing open, unbound generic types. To do that you would need a way of specifying the generic type argument in the inspector for each element. That would involve checking the generic type constraints and probably doing some reflection. Usually the base type would derive from ScriptableObject, in my mind, so that you can assign various instances of different types to the list by dragging and dropping them.
Your situation has another issue however, serializing an interface. You need to use the [SerializeReference] attribute to serialize interface implementations. Unity has no editor UI support for it by default because there is no way of knowing all of the potential classes that implement the interface without using reflection, and then you have to provide a picker in the inspector so that you can select which implementation of the interface to serialize because they might have to be serialized differently. Like, one may have properties that another implementation doesn’t. One may be a UnityEngine.Object, like a ScriptableObject, and another may not be. It could just be a serialized pure C# class. So, you have to either build your own custom inspector / editor support, or acquire a plugin that has already solved that issue in one way or another.
Firstly, don’t use both SerializeField and SerializeReference on the same field. You should never mix up serialisation types (this includes Odin serialisation too).
Secondly, the error is probably being drawn by Odin while it’s probably serializing fine. What version of Odin are you using?