- Home /
How to restore EditorWindow property after playmode?
I just started experimenting with a custom EditorWindow and I added a simple toggle button to enable/disable MeshRenderer.enable. Everything works just fine, however, after returning from playmode, the state of the button still persists. I thought everything got serialized and restored when returning to Edit mode? At least that's what happened to the MeshRenderer.enable. How can I restore the state of the toggle button pre-Playmode so everything is in sync?
Here's the sample:
public class TestEditor : EditorWindow
{
private bool EnableButton = true;
void OnGUI()
{
EnableButton = EditorGUILayout.Toggle("ToggleMesh", EnableButton);
dosomething();
}
}
Is there a doc page that explains how the saving/reloading of properties work with respect to playing the scene from within the editor and then returning back to editing mode?
Sadly, I have the same issue. I searched far and wide and found no solution. Even experts here told me only "Editor" are resistant to this issue, not EditorWindow. The solution I implemented is not what I would consider "clean", but it works. I've added metadata on all GameObject of my level. Each object in a level get a specific unique ID that doesn't change between play states. When the game start or stop, my EditorWindow retrieve it's data from the scene.
Answer by Bunny83 · Sep 18, 2013 at 09:20 PM
EditorWindows are serialized the same way all other objects are serialized. That means only public variables (or private variables with SerializeField) of a serializable type are "saved" and "restored". Another way is to use OnEnable / OnDisable and EditorPrefs to store information even beyond editor restarts.
The problem is not one of serialization as the objects are properly saved and retrieved. The problem is the data of an object targeted by an EditorWindow on play state changes. For some obvious reason, Unity creates a copy of all the level's GameObject to build its playfield when you start the engine. For this reason, GameObjects may even change ID between play state. If you have a EditorWindow who's targeting a GameObject in your scene, it won't reference the proper data once the play state changes. It's weird because it still reference the proper object, but not the proper data. An example of this, I added some debug line on my EditorWindow, the data of an enum in a ScriptableObject was different in my window vs the same object seen in the Inspector. I knew it was the same ScriptableObject because it was my EditorWindow who called the Inspector! On top, I haven't found any built-in way or method to track a GameObject between changes of play states.
Well, it seems you actually don't have the same issue because the question was about data being lost at playmode-change. The editor and the scene runtime are two seperate things. Of course if you reference an asset inside an editor window the reference won't be updated at playmode change since the Editorwindow doesn't belong to the same serialization-set. The inspector works the same way, only the Selection class tracks the selected object, but that's based on the hierarchy item and not a normal reference.
So you simply have to live with the fact that in playmode you work with different objects.
He was talking about a button binded on a GameObject which state persist between play state changes. I doubt he has problem serializing a $$anonymous$$eshRenderer on a GameObject.
I fully understand the difference of serialization-set, but Unity doesn't offer any easy solution to have an EditorWindow's data update through play state changes. Frankly, would have it been hard to have a method to retrieve a GO between play state?
Or even assume that someone somewhere would wish an EditorWindow to update its reference?