x


How do I have an EditorWindow save it's data inbetween opening and closing Unity? C#

I am trying to make an EditorWindow that will store global data about my application, data which I want every level to be able to access. So my first route of going about this was to just make a static public class GlobalData. In it is all the data I want to store. Then in my EditorWindow I do a simple GlobalData.whatever this works in between opening and closing the EditorWindow. But not inbetween opening and closing Unity. It seems to not save changes to the variables in my static class. I do have [System.Serializable] put about my static class, and I have even tried calling SetDirty on my GlobalData data, but that doesn't work.

From the advices of someone else I tried to create GlobalData NOT as a static, but rather just public. Then I create an Instance of GlobalData in my EditorWindow Class. But this does not save the data between opens and closes of even just the EditorWindow! I assume of course because the instance of GlobalData is being made in the EditorWindow so it loses that instance when it is closed.

I've tried messing around with making my GlobalData class a ScriptableObject, but that doesn't work.

I've seriously spent like the past 18 hours sitting here trying any and everything I can think of and am running out of ideas :( Please Help. If anyone could just give me the most base simple example code for an EditorWindow that somehow saves it's data between open and closes of Unity, I would be so grateful. I cannot find any example projects anywhere that show how to do this.

And here is the code I've made thus far:

using UnityEngine;
using UnityEditor;

public class PanoSetup : EditorWindow
{
    [MenuItem ("Window/PanoSetup")]
    static void Init () {
        //PanoSetup window = 
       EditorWindow.GetWindow (typeof (PanoSetup));
    }

    void OnGUI () {
       GlobalData.texture = (Texture2D) EditorGUILayout.ObjectField(
            "Find Dependency",
            GlobalData.texture,
            typeof(Texture2D)); 
    }
}

using System.Collections;
using UnityEngine;

[System.Serializable]
static public class GlobalData {

    static public string texturePath = "temp";
    static public int what = 1;
    static public Texture2D texture = null;

}
more ▼

asked May 22 '11 at 06:20 AM

eem gravatar image

eem
201 21 23 25

It's a pity Unity doesn't automatically save an asset holding the serialized properties of an EditorWindow.

Mar 13 at 03:35 AM yoyo
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Use EditorPrefs to save and load values. Load them in OnEnable and write them in OnDisable and you're done.

more ▼

answered May 22 '11 at 06:35 AM

Jake L. gravatar image

Jake L.
943 5 6 22

Right, the best way. Or maybe use static variables but it just "saves" the values until Unity is closed. EditorPrefs saves it actually on disk ;)

May 22 '11 at 10:24 AM Bunny83

I wanted to use statics like that...

I noticed something new, if you make a ScriptableObject Class, or a class that extends nothing. When you click on that script inside of the project pane, you can see the public properties like Texture2D in the inspector! So clearly such a class is capable of storing data input to it form external sources while only being in the project pane. I however could not figure out how to get at this data. Object.FindObjectOfType will succesfully find a ScriptableObject in the project pane and load it, and get data from it, but I could not put data back into it.

May 22 '11 at 09:56 PM eem

Have you read my answer? AssetDatabase.CreateAsset will do what you want (i guess :D). You have to store your object as ".asset"

May 24 '11 at 04:44 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

Well, i just saw that you want to store complex types (like texture references). Well another way would be to create a separate script that saves your variables to your assets. Just create a GameObject (set the hideflags) and attach your script to it (all done from within your editor-script). Use AssetDatabase and EditorUtility to save and load it as prefab.

more ▼

answered May 22 '11 at 10:40 AM

Bunny83 gravatar image

Bunny83
45.2k 11 49 207

Well, i guess a class that is derived from ScriptableObject should also be possible to save as asset.

May 22 '11 at 10:42 AM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4150
x276
x169
x66
x54
x6

asked: May 22 '11 at 06:20 AM

Seen: 3549 times

Last Updated: Mar 13 at 03:35 AM