Saving game object references in an EditorWindow

So I have created an EditorWindow and have some ObjectFields in it with references to certain game objects.

Now when I close the editor I want to serialize this data. How do I do it?

I have read a lot of different threads on the subject. Using the instance id of a gameobject doesn’t seem to work since it changes when restarting the scene/unity. Also the path of the game object only works if no other game object have the same path.

When using an inspector this is a lot easier since you can create a public variable and it will be auto-saved by Unity
but for EditorWindows, how should I do it? The only ways of saving things are through EditorPrefs.SetString(…) etc.

Anyone found a way?

Have you thought of using a ScriptableObject instead of storing it in the editor directly? You may have a certain path in your project where your ScriptableObject with the reference is. The editor may try to find it with AssetDatabase.LoadAssetAtPath and create it if it is not there. That would be a lot cleaner in my opinion.

Dantus thank you for your input! This was indeed a nice way to save settings and the settings did actually stay even after a pause/play but only when i used EditorUtility.SetDirty(…). The settings are however lost when I change scene or quit Unity. There must be a way to save a reference to an object in a persistant way or am I wrong? One could use the path of the object in the hierachy but if an object is moved/renamed or multiple objects have the same name I would get a problem.

I might post a question in the UnityGUI section instead.

You can not save references to objects from a scene “globally”. If you describe the scenario for which you actually need to save those references, it is easier to understand the situation and find a solution.

Would love to see an answer to this one. Actually, I’m trying to do even more…
I have an EditorWindow that lets you select a gameObject from the scene, and then allows you to either store a reference to it, or see all it’s attached MonoBehaviours and eventually store a reference to one of them. And obviously I can’t find a way to permanently store a reference to neither of them :confused:

I use a context menu for one of my tools.

However I haven’t found out how to save serialized values from a scene in play, after the scene was stopped. The only way I can think of is to save out the values to a editor playerpref and then load them back up when the scene isn’t playing

As people mention here there doesn’t seem to be a good way to get a unique id to an object to you can save it in editorprefs etc.

I came up with a solution yesterday however. What I do is that I create a gameobject in the scene containing saved references etc. I then hide this game object using hideFlags. This allows me to remember references in each scene if I want to!

This is interesting Johot, and kind of similar to what I was unsuccessfully trying (storing them inside a gameObject, but in a List). I’m gonna try it immediately, and thanks for sharing :slight_smile:

Let me know how things are going. I created a monobehavior which works like a singleton so when I get the instance of the singleton it creates the gameobject and attatches the script if it does not already exist in the scene.

Whew, perfect! Actually, I realized that I’m an idiot. I was trying to serialize a System.object instead than a UnityEngine.Object. I must be very tired today.

What I’m doing in my editor window, is asking users to create an instance of the “manager” gameObject if it doesn’t exist. In my case I prefer that way, cause I don’t like hidden stuff. After that, I use that “manager” to store lists of classes that contain references to gameObjects or MonoBehaviours (chosen by users), and it works perfectly. Whew (I’m trying to make a visual editor for my tween engine :-P)

Thanking for clearing my mind :slight_smile: