- Home /
Edit child in parent list [Custom Inspector]
As question suggests,
I have
[Serializable]
public class Parent
{
public float myFloat;
}
[Serializable]
public class Child : Parent
{
public string childString;
}
public class MonoClass : MonoBehavior
{
public List<Parent> myList;
}
I have MonoClass attached to a gameobject. I can edit the list of Parent but if there's a Child in that list, I can't edit the Child (I can't assign values to childString).
If I have a list of Parent objects, how can I basically get every object in the list to show a default editor for each class subtype?
Now... I KNOW this requires a custom editor. I've done my research. However, nowhere have I seen HOW this can be accomplished.
As a stretch goal, I'd like to be able to pick a subtype from a dropdown list to add to the list. That way I can add all eligible types to the list (and then of course, edit them).
Cheers!
Answer by Loius · Feb 18, 2014 at 10:33 PM
The first issue is that Unity does not keep track of subtypes. You have a list of Parents, Unity will serialize that as a bunch of parents and next time you load the program, all the child data will be lost.
To demonstrate lots of subclasses, let's say we have Letter as the parent class, and A, B, C, D, E... Z all extend Letter. You need a SerializableLetter so that Unity keeps track of things properly. It's extremely ugly!
[Serializable] public class Letter { }
[Serializable] public class A : Letter { }
...
[Serializable] public class SerializableLetter {
// Unity can serialize A, B, C, etc, so let's have one of each!
public A a=null; public B b=null; public C c=null; public D d=null;
...
// We're almost never going to use a SeriLetter as anything but a Letter, so let's make it easy:
public static implicit operator Letter(SerializableLetter sl) {
if ( null != a ) return a;
if ( null != b ) return b;
...
if ( null != z ) return z;
}
}
Your MonoBehaviours can then hold lists of SerializableLetters when they need those lists to be saved in prefabs. You can still have lists of Letters; Unity just won't be able to serialize those into prefabs.
You can't let Unity auto-edit your SerializableLetter either, of course. What I did is I created an editor inside the SerializableLetter class itself:
public class SerializableLetter {
...
// This is unnecessary, but it ended up being handy to cache this for me.
[SerializeField] string typeName = "";
#if UNITY_EDITOR
public static void Editor(ref SerializableLetter sl) {
// The Letter editor is capable of creating the subclass objects we need to store:
Letter temp = (Letter)sl;
Letter.Editor(ref temp);
if ( null == temp ) return;
// Determine what type it is, store it in the appropriate variable, and null out whatever was not null so that we have only one active subtype:
typeName = temp.GetType().Name;
if ( typeName == "A" ) a = temp as A; else a = null;
...
if ( typeName == "Z" ) z = temp as Z; else z = null;
}
#endif
}
public class Letter {
public static string[] subtypes = new string[]{
"A","B","C",...,"Z"};
#if UNITY_EDITOR
public static void Editor(ref Letter l) {
System.Type old=null, current=null;
string name = "";
if ( null != l ) { old = l.GetType(); current = l.GetType(); name = old.Name; }
// Show a popup list of all available types (I had some nicety functions to create a list of human-readable names to display instead of subtypes; the index is the only important value for the code):
int curIndex = System.Array.IndexOf(subtypes, name);
curIndex = EditorGUILayout.Popup(curIndex, subtypes);
if ( curIndex > -1 ) current = System.Type.GetType(subtypes[curIndex]);
if ( current != old ) {
// The type was changed - we can't *always* do this, because it would reset all values constantly. It is also guaranteed that current is never null here, because you can't pick an invalid value in EdGUIL.Popup
l = (Letter)System.Activator.CreateInstance(current);
}
// The various subclasses can override AdditionalEdit to create their own editors.
if (null != l) l.AdditionalEdit();
}
protected virtual void AdditionalEdit() { }
#endif
}
This could all be cleaned up a little bit with some better use of lists, but I haven't taken the time to do that yet. The real killer is the massive list of variables in SerializableLetter; I have no idea how to trim that down and still have Unity respect types.
Hey! By the by, do not do this if your classes are nested recursively - that is, if you have class A that contains a list of Letters. Unity will set aside way too much memory for the object and you'll end up with a hard-to-find bug.
Your answer
Follow this Question
Related Questions
Custom Inspector: Targets & GameObjects 1 Answer
Some public attributes not shown in inspector, default references 3 Answers
Dynamic changing type in ReorderableList 0 Answers
Custom inspector field resetting when compile or play 1 Answer
Update variables with C# properties in customeditor 2 Answers