Building a Collection of Subclasses and Modifying Their Public Members from the Inspector
I'm trying to figure out a way to have a component script that I can attach to an object in the scene that holds a collection of references to objects of type BaseClass but would allow me to view and edit the public members of subclasses of BaseClass in the same collection.
For example, I have a component script called CommandList
that I attach to an object in the scene. It has a member public CommandHolder[];
where CommandHolder
looks like this:
[Serializable]
public class CommandHolder
{
public BaseCommand command;
}
Where BaseCommand
is just some base class that we'll say has no public members.
Now in the Unity Inspector if we look at an object with a CommandList
component we'll be able to define an array size for our array of CommandHolder
elements. Then I can drag and drop scripts from my Assets into the public BaseCommand command
field of the CommandHolder
object in the inspector.
So this is where my question comes in. If I have a class called WaitCommand
:
public class WaitCommand : BaseCommand
{
public int seconds;
}
And another called MoveCommand
:
public class MoveCommand : BaseCommand
{
public Vector3 direction;
public float speed;
public float duration;
}
Then what I want is if I drag and drop a WaitCommand
script into the command
field of a CommandHolder
element in my CommandHolder
array then I want to be able to inspect the seconds
public field in the inspector. I understand reason this doesn't happen is because CommandHolder
expects a BaseCommand
object so its only treating our WaitCommand
as its base class and not exposing the public int seconds
member that only exists in the subclass. So then how can I get the behavior I want?
I imagine this would involve some reflection, because I don't want the solution to involve a custom inspector class for each possible subclass of BaseCommand
, I want it to just work.
Answer by StickFigs · Sep 05, 2015 at 07:31 PM
Well I came to an awkward, easily breakable solution for what I'm trying to do. It involves just giving my base class a variety of array structures holding basic data types my subclasses will probably need then having a string[]
for each of these arrays which act as labels for the data object in the same index in the actual data type array. The base class has empty arrays for the string[]
label arrays and each subclass overrides the value of the string[]
related to the data type array it will need a parameter for. Then I just wrote a custom PropertyDrawer
for my BaseAICommand
class which shows only property fields for indexes in the real data arrays which have an accompanying label in the string[]
that goes with that data type array.
That's an awkward explanation but I think the code will clarify what exactly I'm doing:
AICommandHandler.cs
using UnityEngine;
using System.Collections;
// This class attaches to a gameobject in the editor and just holds a list of BaseAICommand
// objects.
// Since I can't drag and drop subclasses into the array and it automatically populates
// Element slots with new BaseAICommand objects then I would eventually have to hide the
// default array controls and add a custom control for instanstiating subclass types
// into the array.
public class AICommandHandler : MonoBehaviour
{
public BaseAICommand[] commands;
}
BaseAICommand.cs
using UnityEngine;
using System.Collections;
[System.Serializable]
public class BaseAICommand
{
public float[] floats = new float[0];
public string[] strings = new string[0];
public Vector3[] vector3s = new Vector3[0];
public int[] ints = new int[0];
public abstract string[] floatsNames = new string[0];
public string[] stringsNames = new string[0];
public string[] vector3sNames = new string[0];
public string[] intsNames = new string[0];
}
MoveAICommand.cs
using UnityEngine;
using System.Collections;
// An example of what a subclass would look like.
public class MoveAICommand : BaseAICommand
{
private float duration;
public MoveAICommand()
{
this.vector3sNames = new string[] { "Direction" };
this.floatsNames = new string[] { "Speed", "Duration" };
}
}
BaseAICommandEditor.cs
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(BaseAICommand))]
public class BaseAICommandEditor : PropertyDrawer
{
private int rows = 0;
private int NamesToGUI(SerializedProperty actualArrayProp, SerializedProperty namesArrayProp, Rect position)
{
int rowsAdded = 0;
var namesArrayPropLength = namesArrayProp.arraySize;
for (int i = 0; i < namesArrayPropLength; i++)
{
var propName = namesArrayProp.GetArrayElementAtIndex(i);
var propNameString = propName.stringValue;
// Stop enumerating if we hit a blank name
if (string.IsNullOrEmpty(propNameString))
break;
actualArrayProp.InsertArrayElementAtIndex(i);
EditorGUI.PropertyField(position, actualArrayProp.GetArrayElementAtIndex(i), new GUIContent(propNameString));
rowsAdded++;
}
return rowsAdded;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
rows = 0;
position.height = 16f;
EditorGUI.BeginProperty(position, label, property);
var floatsProp = property.FindPropertyRelative("floats");
var stringsProp = property.FindPropertyRelative("strings");
var vector3sProp = property.FindPropertyRelative("vector3s");
var intsProp = property.FindPropertyRelative("ints");
var floatsNamesProp = property.FindPropertyRelative("floatsNames");
var stringsNamesProp = property.FindPropertyRelative("stringsNames");
var vector3sNamesProp = property.FindPropertyRelative("vector3sNames");
var intsNamesProp = property.FindPropertyRelative("intsNames");
rows += NamesToGUI(floatsProp, floatsNamesProp, position);
position.y += 18f;
rows += NamesToGUI(stringsProp, stringsNamesProp, position);
position.y += 18f;
rows += NamesToGUI(vector3sProp, vector3sNamesProp, position);
position.y += 18f;
rows += NamesToGUI(intsProp, intsNamesProp, position);
EditorGUI.EndProperty();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return 16f + 18f * rows;
}
}
This actually seems to work but it has some fatal flaws that prevent it from being a good solution to my problem:
AICommandHandler would need a custom Editor inspector view written to handle adding and removing BaseAICommand-derived objects to and from the array.
Overriding the
string[]
values in the constructor method of the subclasses seems like the only place I can set these values. The problem here is that if I ever need to modify these values on a subclass then I would have to go through my entire game and delete and recreate any instances to have them be created with the new defaults. THIS is a showstopper.This solution requires two arrays for each data type class that I might want to be used by a subclass. This could get out of hand very easily.
Thoughts?
Your answer
Follow this Question
Related Questions
Custom Editor List with child classes 1 Answer
Show variable of class that dont inherit from MonoBehaviour in Inspector 1 Answer
Popup to choose which child class I want in Custom Editor Inspector 0 Answers
Custom Editor Inspector member class with inheritance, values resets after starting Play Mode 1 Answer