Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by eem · May 22, 2011 at 06:20 AM · c#scriptableobjecteditorwindowstaticsave datastate

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;
     
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yoyo · Mar 13, 2013 at 03:35 AM 0
Share

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

5 Replies

· Add your reply
  • Sort: 
avatar image
14

Answer by ThomLaurent · Apr 02, 2018 at 02:06 AM

You could use Unity's serialization combined with JsonUtility and EditorPrefs to save your fields and then load them up in just 4 lines :

 public class AnExampleWindow : EditorWindow
 {
     [SerializeField] private Vector2 _someV2Field = Vector2.zero;
     [SerializeField] private Color _aBitOfColor = Color.green;
     [SerializeField] private List<Color> _aFlagColors = new List<Color> { Color.blue, Color.white, Color.red };
     [SerializeField] private bool _amIFrench = true;
     [SerializeField] private string _savePath = "Assets/Resources/anFlag.png";
 
     private string _aNotSerializedString;
     private int _aNotSerializedInt;
 
     protected void OnEnable ()
     {
         // Here we retrieve the data if it exists or we save the default field initialisers we set above
         var data = EditorPrefs.GetString("AnExampleWindow", JsonUtility.ToJson(this, false));
         // Then we apply them to this window
         JsonUtility.FromJsonOverwrite(data, this);
     }
 
     protected void OnDisable ()
     {
         // We get the Json data
         var data = JsonUtility.ToJson(this, false);
         // And we save it
         EditorPrefs.SetString("AnExampleWindow", data);
 
         // Et voilà !
     }
 
 
         // Your pretty code here...
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image phobos2077 · Jun 08, 2018 at 08:00 AM 1
Share

This is the best solution I$$anonymous$$O.

avatar image
6

Answer by Diet-Chugg · Apr 17, 2015 at 06:04 PM

There are several ways to do this. I would create a/several ScritpableObject and store them in your project's Resources folder.

I'd make a class:

 public class MyScriptableObjectClass : ScriptableObject
 {
 //Savable data here
 }


Than I would save and load it using:

 MyScriptableObjectClass myInstance = (MyScriptableObjectClass )Resources.Load("myInstance.asset") as MyScriptableObjectClass;
 if(myInstance  == null)
 {
 myInstance = CreateInstance<MyScriptableObjectClass>();
  AssetDatabase.CreateAsset(myInstance , "Assets/Resources/myInstance.asset");
  AssetDatabase.SaveAssets();
          AssetDatabase.Refresh();
 }

In The Game you could load the class from anywhere using MyScriptableObjectClass myInstance = (MyScriptableObjectClass )Resources.Load("myInstance.asset") as MyScriptableObjectClass; and access the data there.

(Edit: Quick Note: The code I wrote is not tested code. I don't know if it will work if you just copy paste it. But it will point you to the right classes to use.)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image StormMuller · Dec 15, 2017 at 06:48 PM 3
Share

This is quite an old question, but it's the first one that comes up when doing a google search. Scriptable objects are a better solution that editor prefs for 3 reasons.

  1. They can be shared between machines, projects and can be packaged.

  2. They're easier to debug because you can see the saved data in the inspector.

  3. Unity seems to do this and seems to be their standard. For example lighting data and navigational data are both stored as assets.

avatar image
5

Answer by Jake-L · May 22, 2011 at 06:35 AM

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · May 22, 2011 at 10:24 AM 1
Share

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 ;)

avatar image eem · May 22, 2011 at 09:56 PM 0
Share

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.

avatar image Bunny83 · May 24, 2011 at 04:44 PM 0
Share

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

avatar image fseydel · Apr 17, 2015 at 10:35 AM 1
Share

Link updated: EditorPrefs

avatar image Bunny83 · Apr 17, 2015 at 11:34 AM 0
Share

@fseydel:
Thanks for pointing out that broken link. I've fixed it in the answer.

avatar image
1

Answer by Bunny83 · May 22, 2011 at 10:40 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · May 22, 2011 at 10:42 AM 0
Share

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

avatar image
0

Answer by Veehmot · Jun 02, 2013 at 09:13 PM

This blog post tells you all about serializing objects for Editor scripts.

http://blogs.unity3d.com/2012/10/25/unity-serialization/

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image greggman · Nov 25, 2014 at 06:27 PM 2
Share

That example doesn't work for me. The moment I close the window everything is lost. Any idea where I can find an example that works?

avatar image Quatum1000 · Mar 27, 2017 at 03:01 PM 2
Share

This example is a mess. It does not provide a solution, it only shows the trouble of this try

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prevent changes in ScriptableObject type Asset in Editor. Dont save it. 0 Answers

Multiple Cars not working 1 Answer

EditorWindow properties in Game Code 1 Answer

Distribute terrain in zones 3 Answers

When is a static game object static? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges