Return value from EditorWindow...?

I’m trying to take a value (or set of values, changes, etc.) from one EditorWindow and reflect those changes back to the editor and subsequently the object the editor is representing.

I feel like I’m missing something very fundamental and basic–and maybe its documented some place and I was just unable to find it. I’ve thumbed through the answers and glanced through some code but I didn’t find what I was looking for. There doesn’t seem to be any information that explicitly addresses this.

I suspect that I have to do one of the following:

  • Have a reference to the object be passed along between Editor and EditorWindow
  • Implement return callback (though I haven’t seen this model used anywhere that I could tell).
  • …or rely on an event driven model to send arguments between one window and another.

If anyone could help me out in understanding the basic principal behind communication between editors, editor windows, and the objects that they represent that would be much appreciated.

Specifically, what I have is this: a behavior which has inspector elements allowing me to open a preview window for a specific type of custom asset, where upon clicking “OK” the window will dismiss itself and whatever values were set or changed in the preview window will be applied to the behavior whose editor enabled the launch of the window in the first place.

public class MyBehaviorEditor : Editor {
  ...
  public override void OnInspectorGUI() {
    ...
    EditorGUILayout.BeginHorizontal();
    EditorGUILayout.Space();
    if (GUILayout.Button("Load", GUILayout.Width(50))) {
      MyBehaviorLoaderWindow window = (MyBehaviorLoaderWindow)
              EditorWindow.GetWindow(typeof(MyBehaviorLoaderWindow));
    }
    EditorGUILayout.EndHorizontal();
  }
}

and…

public class MyBehaviorLoaderWindow : EditorWindow {
  ...
  public override void OnInspectorGUI() {
    ...
    if (GUILayout.Button("OK", GUILayout.Width(50))) {
      // What to put here to return a value back to MyBehaviorEditor, if anything?
    }
  }
}

Well, you have to find your own way to handle such information exchange. The main problem is that a custom inspector and an editor window have some restrictions:

  • EditorWindows can’t be modal, so the user can do other things in the background, like selecting another object.
  • Custom inspectors are created and destroyed automatically by Unity. If the user deselects the current object the editor instance may get destroyed. Therefore you can’t rely on the fact that the inspector is still there or the selected object is still the same.

The best way would be to submit the selected object to the EditorWindow when it’s opened. Save your own reference to the selected object in the editorwindow.

Do your changes to the object directly in the EditorWindow class.


Modal windows would be a nice feature but at the moment you can’t do something like that.

ps. EditorWindows doesn’t have a OnInspectorGUI method, only OnGUI like MonoBehaviours