Hello guys,
I am struggling a lot to fix this assertion and I am not able to find the problem. Let’s see if you guys can help me with it. I am leaving below the CustomPropertyDrawer code that I am using:
[CustomPropertyDrawer(typeof(SubclassSelectorAttribute))]
public class SubclassPropertyDrawer : PropertyDrawer
{
private VisualElement _container;
private SerializedProperty _property;
private Dictionary<string, Type> _keyToType;
private List<PropertyField> _propertyFields;
private static int _index = 0;
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
_container = new VisualElement();
_property = property;
_keyToType = new Dictionary<string, Type>();
_propertyFields = new List<PropertyField>();
Debug.Log($"{_index++}: Property {_property.displayName}");
var baseType = fieldInfo.FieldType.IsArray ? fieldInfo.FieldType.GetElementType() : fieldInfo.FieldType;
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var derivedClasses = new List<string>();
int typeIdx = 0;
int typeCounter = 0;
foreach (var assembly in assemblies)
{
if (assembly.GetName().Name == "Assembly-CSharp")
{
var types = assembly.GetTypes();
foreach (var type in types)
{
if (type.IsSubclassOf(baseType))
{
derivedClasses.Add(type.Name);
_keyToType[type.Name] = type;
if (property.managedReferenceValue?.GetType() == type)
{
typeIdx = typeCounter;
}
typeCounter++;
}
}
}
}
var dropdown = new DropdownField("Skill Type", derivedClasses, typeIdx);
dropdown.RegisterValueChangedCallback(v => OnDropdownValueChanged(v.newValue));
_container.Add(dropdown);
Show(derivedClasses[typeIdx]);
return _container;
}
private void OnDropdownValueChanged(string value)
{
ResetPropertyFields();
Show(value);
_container.Bind(_property.serializedObject);
}
private void Show(string value)
{
if (_property.managedReferenceValue?.GetType() != _keyToType[value] || _property.managedReferenceValue == null)
{
var subclassInstance = Activator.CreateInstance(_keyToType[value]);
_property.managedReferenceValue = subclassInstance;
}
var propertiesIterator = _property.Copy().GetEnumerator();
while (propertiesIterator.MoveNext())
{
if (propertiesIterator.Current is SerializedProperty childSerializedProperty)
{
if (childSerializedProperty.depth <= _property.depth + 1)
{
var propertyField = new PropertyField(childSerializedProperty);
_container.Add(propertyField);
_propertyFields.Add(propertyField);
}
}
}
_property.serializedObject.ApplyModifiedProperties();
}
private void ResetPropertyFields()
{
if (_propertyFields.Count > 0)
{
foreach (var propertyField in _propertyFields)
{
_container.Remove(propertyField);
}
_propertyFields.Clear();
}
}
}
I am basically trying to allow derived classes to be selectable on inspector. Everything goes fine during initialization and the assertion does not pop up, as you can see in the pictures below:

But as soon as I add a new element to the array, the assertion pops up:

Could you please point me out what I am doing wrong? I guess I may be misunderstanding something about how the GUI is drawn or the serialized objects are updated… but I am not able to catch the bug.
Thank you very much in advance!








