Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Best practice for saving data from an EditorWindow for use at runtime?

Discussion in 'Scripting' started by casimps1, Jul 31, 2014.

  1. casimps1

    casimps1

    Joined:
    Jul 28, 2012
    Posts:
    254
    I'm working on an editor extension that will allow the user to specify some data in an EditorWindow. I then need to somehow store that data and then retrieve it later at runtime.

    PlayerPrefs is great for persisting data from one runtime play session to the next. EditorPrefs is great for persisting data from one editor session to the next. But I'm a little stumped about how to best persist data from editor to runtime.

    Currently I'm thinking about creating a ScriptableObject class to use as a data container, and then creating an asset instance of that class in the Assets folder (side note - really wish I could create an instance of a ScriptableObject without having to write code). Then, I guess I'll reference that ScriptableObject from an in-scene GameObject so that it gets packaged into the build.

    Seems reasonable to me, but feels a little overcomplicated. Is there a better way?
     
  2. MakeCodeNow

    MakeCodeNow

    Joined:
    Feb 14, 2014
    Posts:
    1,246
    Yeah, ScriptableObject(s) referenced by in-scene object(s) is absolutely the way to go. It'll work properly with Unity's referencing, packaging, meta files, and everything else.
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    ScriptableObjects work great, but if you can save the data instead in the current scene or as a prefab (if this fits with the type of data you're generating), it's one less ScriptableObject that the user has to keep track of.
     
  4. casimps1

    casimps1

    Joined:
    Jul 28, 2012
    Posts:
    254
    Well, saving it in the current scene doesn't make sense if the data is intended to be game-wide (as in my case).

    Saving it in a prefab is an interesting idea that I hadn't considered. Of course, then I would have to rely on the user to not accidentally override prefab default values on any in-scene instances. Since I'm trying to create an extension to sell on the Asset Store I'm trying to create an idiot-proof architecture.
     
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    ScriptableObject if it's only a data container.

    Prefab if it's a collection of polymorphic smaller container - MonoBehaviou - bunched together.

    As for ScriptableObject, I would advice you to make some code that you can reuse for any ScriptableObject you have to create, so you do it once, and never have to write a "creator" of SO again.