opsive
1
Hello,
Is it possible to get a generic class to appear in the editor? I have the following:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class GenericTest<T> {
public T content;
public float aFloat;
}
public class Test : MonoBehaviour {
public GenericTest<int> genericObject;
}
When I assign Test as a component, genericObject is not shown in the editor. If I completely remove the generics code, genericObject does appear in the editor. I am using Unity 3 beta 5.
Thanks,
Justin
As I was also looking to inspect/serialize a custom generic class, I found a work around for my use case. I don't think this will be too generally applicable; but it might work for others so I thought I'd share.
If, like me, you are writing a generic class but only intend to use it for a single type (or a small handful), you can make a new class that extends the generic for that specific type. Unity will then show instances of the class in the inspector and serialize them. Obviously not an ideal solution but it fits my needs and I can live with the hack.
public class IntTest : GenericTest<int> {
}
Unity will inspect/serialize IntTest properties even though it won't for GenericTest< int> ones.
Hey Justin,
Unfortunately Unity does not support serialization of generic types yet, other than `List`.
inspector should know the type of properties (class variables) to be able to show the correct slots for them. components should be a children of monobehaviour and the type of all variables that you want to show them in the inspector must be known before runtime. even in js you can not use dynamic variables.