I have the following class:
public abstract class Binding : MonoBehaviour
{
public ViewModel ViewModel;
public string ViewModelPropertyName;
protected virtual void Awake()
{
VMManager.RegisterBinding(this);
}
public abstract void UpdatedBinding(object value);
}
And his custom inspector:
[CustomEditor(typeof(Binding))]
public class BindingEditor : Editor
{
//View Model
private ViewModel[] viewModelList;
private List<string> viewModelListStrings = new List<string>();
private int viewModelListIndex;
//Properties
private List<string> viewModelPropertyListStrings = new List<string>();
private int viewModelPropertyListIndex;
private SerializedProperty viewModel;
private SerializedProperty viewModelPropertyName;
private void OnEnable()
{
viewModel = serializedObject.FindProperty("ViewModel");
viewModelPropertyName = serializedObject.FindProperty("ViewModelPropertyName");
viewModelList = (target as Binding).GetComponentsInParent<ViewModel>();
int index = 0;
viewModelListStrings.Clear();
foreach (ViewModel vm in viewModelList)
{
if(vm == viewModel.objectReferenceValue)
{
viewModelListIndex = index;
}
viewModelListStrings.Add($"{vm.GetType().Name}({vm.name})");
index++;
}
viewModel.objectReferenceValue = viewModelList[viewModelListIndex];
GetScriptProperties(viewModel.objectReferenceValue.GetType());
}
public override void OnInspectorGUI()
{
serializedObject.Update();
//View Model
int lastviewModelIndex = viewModelListIndex;
viewModelListIndex = EditorGUILayout.Popup("View Model", viewModelListIndex, viewModelListStrings.ToArray());
if(lastviewModelIndex != viewModelListIndex)
{
viewModel.objectReferenceValue = viewModelList[viewModelListIndex];
GetScriptProperties(viewModel.objectReferenceValue.GetType());
}
//Property
int lastviewModelPropertyIndex = viewModelPropertyListIndex;
viewModelPropertyListIndex = EditorGUILayout.Popup("Property", viewModelPropertyListIndex, viewModelPropertyListStrings.ToArray());
if (lastviewModelPropertyIndex != viewModelPropertyListIndex)
{
viewModelPropertyName.stringValue = viewModelPropertyListStrings[viewModelPropertyListIndex];
}
serializedObject.ApplyModifiedProperties();
}
private void GetScriptProperties(System.Type viewModel)
{
PropertyInfo[] viewModelPropertyList = viewModel.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly);
int index = 0;
viewModelPropertyListStrings.Clear();
foreach (PropertyInfo property in viewModelPropertyList)
{
if (viewModelPropertyName.stringValue == property.Name)
{
viewModelPropertyListIndex = index;
}
viewModelPropertyListStrings.Add(property.Name);
index++;
}
viewModelPropertyName.stringValue = viewModelPropertyListStrings[viewModelPropertyListIndex];
}
}
The line that is giving me problems is this:
PropertyInfo[] viewModelPropertyList = viewModel.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly);
This code works and the line above returns 1 property, so everything works as expected. But I need to show the custom inspector in child classes of Binding.cs, so I changed the [CustomEditor(typeof(Binding))]
to [CustomEditor(typeof(Binding), true)]
adding the editorForChildClasses as true.
Now the custom inspector works in child classes of Binding, but after this change, the GetProperties method is not working anymore, it always returns 0 properties. If I change the editorForChildClasses again to false, it works again.