- Home /
Editor Control Automagically "Filling" in Data
Hi,
I'm building an editor extension that includes inspecting and modifying the fields/properties of ScriptableObjects, almost exactly like Unity's inspector window. I have an issue when I switch inspector targets and it's wracking my brain trying to fix it. Clicking in the editor view causes a node (or no node) to be selected, which also executes an inspector window callback. The inspector window receives a reference to the selected object in this callback and
Destroys the old inspector (which itself is a ScriptableObject)
Determines the appropriate inspector to display (user-scripted, auto-generated, or none)
Creates an instance of the applicable inspector type and assigns it to the window
Repaints at the end of the execution path
The inspector window's OnGUI function calls the inspector's OnInspectorGUI behavior (if it exists) and repaints.
The issue arises when I edit the value in a control backed by one ScriptableObject's field, switch to a different ScriptableObject without moving keyboard focus away from that control, and then resume focusing the "equivalent" control on the new ScriptableObject. I've included a video demonstrating what happens: http://webmshare.com/6wOGj
This is what information I've collected about the unintended behavior:
It does not affect the SerializedObject backing the inspector until I begin editing the field. You can see this in the video. When I start typing the name of the node in the editor takes the value of the control.
It affects all of my inspectors and every control type. The video features an auto-generated inspector, but I knew about this issue from early testing with custom inspectors. So long as I modify controls shared by two nodes, it happens.
I think has to do with keyboard focus. If I do anything that removes keyboard focus from the control, the next inspector doesn't receive the ghost value. This isn't fixed by setting GUIUtility.keyboardControl to 0 in the callback or in OnLostFocus, though.
The control IDs of two "equivalent" controls are the same. In my example, both name text fields are ID 239.
GUI.changed is not true when the ghost value appears, so it is merely cosmetic.
I feel like the solution to my issue is either so simple that I am overlooking it, or something internal that I'm missing. Hoping for the former.
Answer by Bunny83 · Jul 26, 2016 at 08:41 PM
Well, setting the keyboardControl to "0" should work as the editor would then reinitiate it's text based on the passed text. You can try to also set EditorGUIUtility.editingTextField
to false. This will directly set a value inside the recycled text editor that Unity uses internally.
The textfield actually has this code:
if (editor.controlID == id && GUIUtility.keyboardControl != id)
{
editor.controlID = 0;
}
As as soon as the text field is called while keyboardControl is not the id of this control it would reset the editor controlID. Are you sure that your GUI receives the layout event?
It seems your system is quite complex. What isn't really clear is do you actually use Unity's Inpectorwindow or do you have created your own EditorWindow for that?
This helped!
The inspector window, which is my own EditorWindow and not Unity's, is now using
void OnLostFocus() {
...
EditorGUIUtility.editingTextField = false;
...
}
which eli$$anonymous$$ated the documented side effect. The window is receiving all events, including layout. Thank you!
Your answer
Follow this Question
Related Questions
ShaderGraph-like EditorWindow 0 Answers
How to get unity3d editor account id or unique id 0 Answers
Queue method to be called after asset processor is done 0 Answers
Trigger an event from editor script? 0 Answers
How can i create a pup-up menu like the one for choose the shader on materials in unity 2019? 1 Answer