- Home /
How to create an inspector settings window?
I'm creating a plugin that needs to be configured before using. I've created a standar EditorWindow
with the configuration parameters, that works correctly, but it would be nice to create a settings windows that shows in the Inspector, like the settings windows of Unity3D itself; for example, a window like the one that shows up when you click in Edit/Project Settings/Editor menu item (attached screenshot)! editor settings windows as shown in the Inspector
Any idea how to implement this?
It is a general Editor Window. The labels use EditorStyles.boldLabel, a 2 popups and an input text field.
And how can you dock it into the Inspector? I mean, the inspector itself is a dockable window which changes contents depending on the selected item, ok. So how can I mimic the behavior of any of the Project Settings menu items? When I click any of those items, the inspector gets focused and shows the controls needed to configure each specific item.
The normal object inspector changes based on selection. This is done using a custom Editor (not window) for that object type. If you want to update your window on selection change, there is a built in message, plus other messages to handle basic window events. Editor Windows do not get mouse events, an Editor will.
Thanks @OperationDogBird, I'll dive in the documentation of Custom Editors then.
Answer by Jamora · Jan 21, 2014 at 02:32 PM
To get an inspector window like those of the Unity Editor, you will have to make your settings-object a ScriptableObject, then make a custom inspector for that class.
I feel these code snippets will convey more than several paragraphs of explanation:
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Settings))]
public class SettingEditor : Editor {
private Settings _target;
[MenuItem("testt/testtt")]
public static void Getter(){
/*
** You would naturally get the existing Settings rather
** than create a new one. E.g. using
** Resources.FindObjectsOfTypeAll or
** a manager that maintains a reference
*/
Selection.activeObject = ScriptableObject.CreateInstance<Settings>();
}
void OnEnable(){
_target = (Settings)target;
}
public override void OnInspectorGUI ()
{
_target.settingsValue = EditorGUILayout.IntField("Setting 1",_target.settingsValue);
}
}
using UnityEngine;
public class Settings : ScriptableObject {
public int settingsValue;
}
Answer by MakinStuffLookGood · Jan 21, 2014 at 01:38 PM
You'll probably want to refer to this page from the docs: http://docs.unity3d.com/Documentation/Components/editor-EditorWindows.html
Specifically this snippet here:
// Add menu item named "My Window" to the Window menu
[MenuItem("Window/My Window")]
public static void ShowWindow()
{
//Show existing window instance. If one doesn't exist, make one.
EditorWindow.GetWindow(typeof(MyWindow));
}
This will put your window in Window>My Window, and you can define what is in your window using OnGUI()
Thanks, I've already created a window, but is a dockable Unity3D window. I'm looking how to create an inspector window or how to embed a EditorWindow so it shows in the inspector.
Answer by OperationDogBird · Jan 21, 2014 at 01:54 PM
using UnityEditor;
using UnityEngine;
public class Settings : EditorWindow
{
private int selected = 0;
private string[] opts = new string[] { "Option1", "Option2" };
private static Settings settings = null;
void OnGUI()
{
GUILayout.Label("Version Control", EditorStyles.boldLabel,GUILayout.MinWidth(100));
GUILayout.BeginHorizontal();
GUILayout.Label("Mode", EditorStyles.boldLabel, GUILayout.MinWidth(100));
selected = EditorGUILayout.Popup(selected, opts);
GUILayout.EndHorizontal();
}
[MenuItem("Edit/My Tool/Settings")]
static void GetWindow()
{
settings = GetWindow<Settings>();
}
}
This will create a window with a label and a popup menu. The window is dock-able, so it is exactly like any other window in unitys editor.
Your answer
Follow this Question
Related Questions
SerializedProperty and character arrays 0 Answers
Change the target of inspector from script 0 Answers
Create inspector drop-down button based on the content of a list in editor mode 1 Answer
how to write edit time only logic ? 0 Answers
Editor scripting: Object reference not set to an instance of an object 0 Answers