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

Invoke custom Inspector window without instance?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Senshi, Dec 27, 2014.

  1. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    556
    Hi all,

    I just started playing with a custom editor and came across the need to show settings in the same vein as the Project Settings windows. In fact, that is exactly what I am trying to accomplish!

    Assuming I have a class with all static members, I would like these members displayed in the Inspector when a users clicks Project Settings > My Extension Settings. However, all examples I find show how to write custom inspectors for scene objects, or how to create custom windows, which is all good, except it's another extra window (or panel) that isn't really needed. Is there any way I can just hijack the Inspector window and show my custom editor extension there?

    Thanks and happy holidays,
    Patrick
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Can you store your settings in a ScriptableObject? They can have custom inspectors, too.
     
  3. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    I would suggest doing what @TonyLi said and making a ScriptableObject or if it's editor only ScriptableSingleton. If you really don't want the user to see the object you can always just use HideFlags.
     
    TonyLi likes this.
  4. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    556
    Happy new year everyone, and thanks for the replies! Saving the data in a ScriptableObject or Singleton seems like a plan, thanks. That still leaves the question of how to show those settings inside the Inspector though?
     
  5. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    When you create a ScriptableObject in your project you will be able to click on it and view all the data in the inspector.
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    As @BMayne writes, serializable fields in a ScriptableObject are visible in the inspector just like serializable fields in a MonoBehaviour are. If you don't like the default inspector, you can write a custom inspector editor.
     
  7. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    556
    Thanks for your replies and apologies if I haven't been entirely clear. The step that is missing for me is the "prettifying" of the whole shebang. Ideally I would show these fields when someone clicks a custom entry á la "Project Settings > My Extension Settings", without changing the object selection (both in the hierarchy and the asset view).
     
  8. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    Required Items:
    1. Make ScriptableObject to store all your data in.
    2. A pretty EditorWindow
    Flow:
    • User opens the editor window (MenuItem)
    • User changes a value.
    • Find your ScriptableObject
    • You take the value and set the ScriptableObject instance to that value.
    • ScriptableObject will serialize itself and safe with your project.
     
  9. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    When you select any of the Edit > Project Settings > xxx menu items, it does change the object selection. It selects the corresponding ScriptableObject. These are special ScriptableObjects that Unity keeps in the ProjectSettings folder, which sits next to the Assets folder in your project. You can add your own with a custom editor script:
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class MenuItems
    5. {
    6.     [MenuItem("Edit/Project Settings/My Extension Settings")]
    7.     private static void NewMenuOption()
    8.     {
    9.         Selection.objects = new ScriptableObject[] { yourScriptableObject };
    10.     }
    11. }
    "yourScriptableObject" is a reference to your asset. You could store an instance in the root of Assets. A lot of plugins do that. Although I'm not really a fan of the approach, it certainly works, and it's simple.
     
  10. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    556
    Awesome, thanks so much both! I never even noticed that the standard settings also change the selection. Both seem like fun options to explort, thanks!