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.