Hi, I am working on displaying an abstract class array on inspector depends on ActionType of each element.
My current result as following, I can only display all variables by DrawPropertiesExcluding or display selected variables outside array block…
public class EventHandler : MonoBehaviour
{
public ActionWrapper[] actionList;
private void OnEnable()
{
foreach (var action in actionList)
{
action.actionValue = action.eventAction.CreateAction();
}
}
}
[Serializable]
public class ActionWrapper
{
public EventAction eventAction;
public ActionValueBase actionValue = null;
}
public enum ActionValueType
{
Unknown = -1,
OpenURL = 8,
Scale = 15,
...
}
[Serializable]
public class EventAction
{
public ActionValueType actionType = ActionValueType.Unknown;
public OpenURLAction OpenURLAction = new OpenURLAction();
public ScaleAction ScaleAction = new ScaleAction();
...
public ActionValueBase CreateAction()
{
return GetActionById((int)actionType);
}
public Type GetClassId(ActionValueType type)
{
return GetActionById((int)type).GetType();
}
private ActionValueBase GetActionById(int id)
{
switch (id)
{
...
case 8:
return OpenURLAction;
case 15:
return ScaleAction;
...
}
}
}
[CustomEditor(typeof(EventHandler), true)]
public class EventHandlerEditor : Editor
{
protected EventHandler _eventHandler;
protected SerializedObject _serializedEventHandler;
protected SerializedProperty _serializedActionsProperties;
private void OnEnable()
{
_eventHandler = (EventHandler)target;
_serializedEventHandler = new SerializedObject(_eventHandler);
_serializedActionsProperties = _serializedEventHandler.FindProperty("actionList");
}
public override void OnInspectorGUI()
{
_serializedEventHandler.Update();
DraweventAction();
_serializedEventHandler.ApplyModifiedProperties();
}
protected void DraweventAction()
{
if(_serializedActionsProperties.arraySize > 0)
{
for (int i = 0; i < _serializedActionsProperties.arraySize; i++)
{
SerializedProperty element = _serializedActionsProperties.GetArrayElementAtIndex(i);
EditorGUILayout.LabelField("eventAction", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(element.FindPropertyRelative("eventAction").FindPropertyRelative("actionType"));
EventAction _eventAction = _eventHandler.actionList[i].eventAction;
Type typeOfAction = _eventAction.GetClassId(_eventAction.actionType);
SerializedProperty additionalValues = (element.FindPropertyRelative("eventAction").FindPropertyRelative(typeOfAction.ToString())).Copy();
string parentPath = additionalValues.propertyPath;
while (additionalValues.NextVisible(true) && additionalValues.propertyPath.StartsWith(parentPath))
{
EditorGUILayout.PropertyField(additionalValues);
}
}
}
}
}
I can update display properties by enum but how can I display the selected type of properties only on array element block? And dynamicly update array elements on inspector?
My expectation: