This question borders several previous questions so to be clear: It is not a matter of serialization of generic types, which has been answered elsewhere.
I have a generic class:
public class EnumeratedPalette<T> : AbstractPalette where T : System.IConvertible
{
Color[] colors;
public static int EnumLength {
get {
return System.Enum.GetValues (typeof(T)).Length;
}
}
public static string FieldLabel(int index) {
return System.Enum.GetName (typeof(T), index);
}
protected void Reset()
{
colors = new Color[EnumLength];
}
}
And then I make specific versions of it like this:
public enum FaceColors { Skin, Eye, Hair };
public class FacePalette : ProcPixel.Fundamentals.EnumeratedPalette<FaceColors> { };
They serialize perfectly and work in unity as expected, getting the right length on colors when added to an GameObject. But instead of having a colors be a simple list in the inspector, I would want a generic CustomEditor for EnumeratedPalette so that I can set nice labels on the property-drawers based on the specific enum ( Color is a struct of mine with its own CustomPropertyDrawer which draws whatever label sent to it):
[CustomEditor(typeof(EnumeratedPalette<>), true)]
public class EnumeratedPaletteEditor<T> : Editor where T: System.IConvertible
{
public override void OnInspectorGUI ()
{
int size = EnumeratedPalette<T>.EnumLength;
for (int index = 0; index < size; index++) {
EditorGUILayout.PropertyField (serializedObject.FindProperty ("colors").GetArrayElementAtIndex (index), new GUIContent(EnumeratedPalette<T>.FieldLabel (index)));
}
}
}
Edit / Workaround solution
I made a generic helper class:
public static class EnumEditorHelper<T> where T: System.IConvertible
{
public static void UnpackProperty(SerializedProperty prop)
{
int size = EnumeratedPalette<T>.EnumLength;
for (int index = 0; index < size; index++) {
EditorGUILayout.PropertyField (prop.GetArrayElementAtIndex (index), new GUIContent(EnumeratedPalette<T>.FieldLabel (index)));
}
}
}
And then each CustomEditor can look like:
[CustomEditor(typeof(FacePalette), false)]
public class FacePaletteEditor : Editor {
public override void OnInspectorGUI ()
{
EditorGUI.indentLevel += 1;
EnumEditorHelper<FaceColors>.UnpackProperty(serializedObject.FindProperty("colors"));
EditorGUI.indentLevel -= 1;
serializedObject.ApplyModifiedProperties ();
}
}