I need to access a method within the class for which my custom editor class is helping inspect.
[ CustomEditor ( typeof ( MyClass ) ) ]
public class MyClassInspector : Editor {…}
All the examples that I have found are using FindProperty to get access to the variables defined within MyClass via SerializedProperty. Is there a way that I can get a reference to MyClass, so that I can actually call one of it’s methods? I expected to get it via the “target” variable somehow, but I can’t seem to find the way.
You want to use the target member in the Editor class to access the object being edited by your custom inspector class:
[CustomEditor(typeof(MyClass))]
public class MyClassInspector : Editor
{
private void Foo()
{
// Call method on class who's inspector is modified by this class.
((MyClass)target).Bar();
}
}
A cleaner way would be:
[CustomEditor(typeof(MyClass))]
public class MyClassInspector : Editor
{
private MyClass targetClass
{
get { return (MyClass)target; }
}
private void Foo()
{
// Call method on class who's inspector is modified by this class.
targetClass.Bar();
}
}