When I’m editing my static class using an editor window it all works fine. But when I enter and exit play mode my changes are reverted. How can I fix this?
1 Answer
1You can’t “fix” this because the whole scripting environment is reset when you change playmode. Edit-time and run-time are two different worlds and static variables shouldn’t be used as “permanent” memory because it isn’t permanent. Static variables aren’t serialized. Use non static variables of a serialized object like a MonoBehaviour or ScriptableObject-asset. If you just want to store values for your editor window, use EditorPrefs.
What I want is the script to be able to save it's data without having to be attached to a gameobject.
– slkjdfvIf it's an editor script and you only want to save simple data (int, string, bool) use EditorPrefs. If you have more complex data you might want to save your data as asset by using a ScriptableObject and the AssetDatabase class. Anyway, if you "just" want to preserve the data of an editor window when you change between edit and runtime, just make your variables public and non-static. Editor classes are also serialized, but they aren't saved to disk
– Bunny83@Bunny83 If you have a menu item that needs to manipulate a complex object, your latter solution won't work as far as I know, since menu items must be static, and statics can't access non-statics.
– vargonianI actually did ask my own question today: http://answers.unity3d.com/questions/705165/how-to-prevent-a-menu-operation-from-cancelling-wh.html The object I don't want to die is not a Unity object and not a basic type. It is a really standard C# class, though. My menu item method creates an object which subscribes to the events published by that statically defined class, so if that object dies (on an editor reload), the menu item can't do its job because the event publisher will be destroyed.
– vargonianActually I might be over-complicating it. If I have any object that I create in the menu item class, then run a method using an asynchronous delegate (aka multi-threaded), if the editor reloads, that thread gets destroyed.
– vargonian