- Home /
Generic MonoBehaviour with nested class serialization issue
This serializes:
public class GenericMB<T, V> : MonoBehaviour
{
public T t;
public V v;
}
public class ThisSerializes : GenericMB<int, string>
{
}
i.e. if I have a GO with ThisSerializes
attached to it, 't' and 'v' will show up in the inspector.
This works well too:
public class KvpMB : MonoBehaviour
{
public KVP kvp = new KVP();
[Serializable]
public class KVP : GenericKeyValue<int, string>
{
}
}
public class GenericKeyValue< TKey, TValue >
{
public TKey key;
public TValue value;
}
public class ThisSerializesToo : KvpMB
{
}
Howover; not the case here:
public class GenericMB<T, V> : MonoBehaviour
{
public KVP kvp = new KVP();
[Serializable]
public class KVP : GenericKeyValue<T, V>
{
}
}
public class GenericKeyValue< TKey, TValue >
{
public TKey key;
public TValue value;
}
public class ThisDoesNotSerialize : GenericMB<int, string>
{
}
Attaching ThisDoesNotSerialize
to a GO 'kvp' doesn't serialize thus not visible in the inspector.
I don't get why; I'm not attaching a generic MonoBehaviour; It's just that I'm inheriting from one. It serialized in the first case, why doesn't it serialize in this one as well?
Any ideas?
Thanks.
I even found that having a plain valid nested Serializable class in a generic class does not allow it to be serialized in any derived class.
I couldn't find a restriction of the attribute on $$anonymous$$SDN, so maybe that's a restriction on Unity's side. Since only concrete types can be serialized, maybe such a nested class is not valid since it's containing class is not serializable, too.
Answer by jister · Mar 04, 2015 at 08:07 AM
my guess is because your are not serializing the types only the generics so unity doesn't know what to show
[Serializable]
public class KVP : GenericKeyValue<T, V>
{
}
Your answer
Follow this Question
Related Questions
Start function on a listed non mono script? 2 Answers
Data with reference to scene objects: Monobehaviour or ScriptableObject? 1 Answer
Associate a data object to Monobehaviours with generic 0 Answers
How to correctly use [SerializeReference] on generic types 0 Answers
C# Inheriting from MonoBehaviour but still be able to instantiate with new()? How should I do this? 2 Answers