- Home /
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?
}
}
}
Answer by Bunny83 · Jun 06, 2011 at 04:43 PM
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
Thanks Bunny, I think the OnInspectorGUI in the EditorWindow was a copy and paste error on my part. Good catch, though.
As for submitting a reference to the selected object to the EditorWindow when its opened, I would assume I'd have to do something like (excuse the possible improper syntax, I don't have my Unity project files to reference in front of me):
if (GUILayout.Button("Load", GUILayout.Width(50))) {
$$anonymous$$yBehaviorLoaderWindow window = ($$anonymous$$yBehaviorLoaderWindow)
EditorWindow.GetWindow(typeof($$anonymous$$yBehaviorLoaderWindow));
window.SetObjectReference(target);
}
As far as I can tell from looking at the reference documents and the API there is no way I can pass in the reference at the time of instantiation, it has to be done after the fact, correct?
I'm currently using C# as my language of choice, do you have any thoughts as to the use of delegates to handle "callback" functions from the EditorWindow itself? I suspect that while they may work in some cases they would suffer from the second point you made where an editor instance might be destroyed.
As a quick aside, obviously on the EditorWindow, window, the object reference could be set up as a property so I could assign it using the assignment operator. But still the point stands.