So I have a ScriptableObject with an array of polymorphic objects like this:
[System.Serializable]
public class CMyObject : ScriptableObject
{
public List<CElementBase> _Elements = new List<CElementBase>();
}
[System.Serializable]
public class CElementBase
{
public string _Name;
}
[System.Serializable]
public class CElementInt : CElementBase
{
public int _SomeInt;
}
[System.Serializable]
public class CElementString : CElementBase
{
public string _SomeString;
}
Is there a good way to use the inspector to add and edit derived-class element to the array?
I was able to add the support for adding derived classes using CustomEditor for MyObject and
public override void OnInspectorGUI()
{
DrawDefaultInspector ();
CMyObject myTarget = (CMyObject) target;
if(GUILayout.Button("Add Int"))
{
myTarget._Elements.Add ( new CElementInt() );
}
if(GUILayout.Button("Add String"))
{
myTarget._Elements.Add ( new CElementString() );
}
But even so, the elements that show up in the array only allow me to edit the base class properties (_Name), not the derived class ones (_SomeInt or _SomeString).
Is there a way to have Unity let me edit the actual derived class, or do I need to manually create edit fields for each property based on the type? Please advise!