How to select a MonoBehaviour in inspector from a cusom editor?
Hello there. Essentially, one of my scripts needs a reference to another component, and I want to be able to set in manually in the inspector (i.e. dragging and whatnot). This is all fine and good if the MonoBehaviour field is directly in my Component class, like so:
public class MyComponent : MonoBehaviour {
public MonoBehaviour Reference;
}
However, what I want to do is have the reference inside a scriptable object, as a settings objet for MyComponent, like so:
public class MyComponent : MonoBehaviour {
public SettingsObject Settings;
}
public class SettingsObject: ScriptableObject {
public MonoBehaviour Reference;
// ... and other fields
}
As such, I created a fully functional Custom Editor to have the SettingsObject fields displayed in the inspector:
[CustomEditor(typeof(MyComponent))]
public class MyComponentEditor : Editor {
private MyComponent _component;
private Editor _settingsEditor;
private void OnEnable()
{
_component = (MyComponent)target;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
var settings = _component.Settings;
if (settings != null)
{
using (var check = new EditorGUI.ChangeCheckScope())
{
CreateCachedEditor(settings, null, ref _settingsEditor);
_settingsEditor.OnInspectorGUI();
if (check.changed)
{
doStuff();
}
}
}
}
Visually, the result is mostly the same. In my example, the SettingsObject only contains the reference, but in my project it has a bunch of other fields (of other types, like ints an strings) that get displayed and updated properly and I can adjust.
The issue? I can't select anything for my Reference field. if I hit the bullseye to the right, nothing shows up in the selection window, and when I drag&drop components I can't drop them onto it. I suspect that the "CachedEditor" has something to do with this, but I can't figure out why..
Why does this happen and how can I fix it? Thanks in advance.
EDIT: I also tried both of the following, without an success: CreateCachedEditorWithContext(settings, _component, null, ref _settingsEditor); CreateCachedEditorWithContext(settings, _component.gameObject, null, ref _settingsEditor); I think I'm closer to figuring it out, as "ExposedReference" sounds awfully like something I'm trying to get to.