- Home /
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
Answer by Bunny83 · Jul 30, 2021 at 11:42 AM
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.
I may have not expressed the problem correctly... I do know how to serialize data, and I've done it. What I want to obtain is a behaviour like the default Unity windows (Animator, UIBuilder, etc...); if you notice, Unity default windows, unlike EditorWindow and derived classes, during assemblies reload, do not have frames with the window emptied and I imagine is due to the fact that the window is not being reloaded and I wish to replicate that behaviour (if possible :) )
unlike EditorWindow and derived classes, during assemblies reload, do not have frames with the window emptied
Sorry but I don't get what you mean. My own editor window, specifically the UVViewer I mentioned does not have a single frame empty when an assembly reload happens.
Every window will be serialized, destroyed and recreated when an assembly reload happens. The whole managed Mono environment is shutdown and restarted when this happens. If the state of your window is serialized properly you should not notice any issues. Keep in $$anonymous$$d that static variables are not serialized. This is mentioned in the very last sentence in the script serialization documentation I mentioned in my answer. The Animation window is also just a managed window and is in no way different then any other EditorWindow. So if your window has issues, it's because of your code or that you loose state during the reload because it wasn't serialized properly.
Feel free to try my UVViewer. Just put the file in an editor folder and open the window from the Tools menu. Now just select any object in the scene to view the UV map of that object. If you now edit any of your scripts in the background and switch back to Unity, you will notice the window will stay as it is.
We can't really help you any further with your specific problem since that depends on your specific code. Maybe you use data structures in your editor window that are not serializable?
Your answer
![](https://koobas.hobune.stream/wayback/20220613041007im_/https://answers.unity.com/themes/thub/images/avi.jpg)