Develop an EditorWindow that will not be cleared on assemblies reload (just as the Animator Window)

Hi everyone,
as the title states, I’m developing a development tool for the Unity Editor.
The main difficulty I’ve found is that every time the editor reload the assemblies (a change in the code, entering/exiting play mode and so on) the window is cleared.
I’ve managed to reload everything using the EditorWindow.OnProjectChange function, but clearing and restoring the window I’m loosing all the internal states (toggle values, the position and scale of a GraphView I have in the hierarchy).
I’ve noticed that the Unity Animator window (that is similar to the window I’m building) doesn’t suffer of this problem… How do I achieve this behaviour in my custom window???

Thanks to all

Well is all your internal data serializable? If not, of course the data will be lost on an assembly reload. Note that the EditorWindow class is just a ScriptableObject. It’s literally derived from ScriptableObject. When an assembly reload happens, Unity will serialize all UnityEngine.Object derived classes which are restored after the reload. When your window looses information it’s because it’s not serialized. So the same rules apply to editor windows that apply to runtime scripts concerning hot reloading. See script serialization for more information.

Note that during an assembly reload even private variables are serialized as long as their type is serializable in the first place. Have a look my UVViewer for example. I have several variables that I explicitly do not want to be serialized because they are recreated in OnEnable / OnSelectionChange so I need to mark them as “NonSerialized”.

So if you’re loosing data it’s most likely not serializable.

It seems like Bunny83 is saying that fields will automatically be serialized and should not be lost after domain reload, however, looking at your reply, those values are not serializing even though you both expect them to. One thing you might check is that if your values are inside of a struct, they won’t be serialized until you add [System.Serializable] above the struct.