Search Unity

Difference between an Editor Window and something like the 'Lighting' tab?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Aurecon_Unity, Jan 29, 2016.

  1. Aurecon_Unity

    Aurecon_Unity

    Joined:
    Jul 6, 2011
    Posts:
    241
    I want to create a window that contains a whole bunch of information about the project (non-gaming), and I've been experimenting using Editor Window.

    Everything is working okay, except that if I close the window and re-open it, all the values I enter are lost (labels, booleans etc).

    What I really want is a persistent window like the 'Lighting' panel, that can be opened as a window or docked, and the information remains persistent even when it is closed and re-opened. I want the user to be able to re-open the Project Information window to update information as necessary.

    Is the 'Lighting' window/tab a special type of editor window? Any advice?

    Thanks a lot!
     
  2. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    The lightning window is just an ordinary Editor Window that reads and edits an asset of type "LightmapSettings", which is stored within your scene file.

    You can see this by backup your .unity file, change something, save and then compare the new with the backup. ;)

    So that is a trick you can do as well.. Create a new instance of some ScriptableObject or Prefab, mark it via hideFlags as HideInHierarchy and then only have it editable through your custom window ;)

    If you want Project-wide settings, you can just create an ScriptableObject - Asset (or Prefab with a special MonoBehaviour) somewhere in your project folder and use this as storage. Of course, you could also just dump data in a binary or text file, if you don't feel like using ScriptableObject...

    You could even store your values in EditorPrefs, if you want to keep them synchronized between different projects.
     
    Aurecon_Unity likes this.
  3. Aurecon_Unity

    Aurecon_Unity

    Joined:
    Jul 6, 2011
    Posts:
    241
    Thanks Immanuel, a lot of good info to chew on there.
     
  4. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    In this context, I want to advertise JSONUtility class that was recently made public by Unity (I think since 5.3.0).

    Its especially useful if you have a simple data-holding class that does not inherit from anything (more specific not from MonoBehaviour or ScriptableObject).

    The easiest way to backup/restore a pure data class like this would be:

    Code (csharp):
    1.  
    2. class SomeDataClass
    3. {
    4.     public int someInt;
    5.     public string someString;
    6. }
    7. SomeDataClass myData;
    8.  
    9. void OnDisable()
    10. {
    11.     // backup myData
    12.     File.WriteAllText("myStorage.json", JSONUtility.ToJson(myData, true)); // last parameter makes the file human-readable ;)
    13. }
    14.  
    15. void OnEnable()
    16. {
    17.     // load myData
    18.     myData = JSONUtility.FromJson<SomeDataClass>(File.ReadAllText("myStorage.json"));
    19. }
    20.