Continuing subject of building tools in Unity, today we are going to build custom window in Unity Editor. If you’ve missed the last post about building a custom component inspector, I would recommend checking it first. ?
To build custom tools in Unity, we have to prepare a custom window in Editor. We can achieve that by using the EditorWindow class.
Like with a custom inspector for components, and here we have to use the old GUI system. There is just one difference between building inspector and window. In inspector, we were using OnInspectorGUI() method and we could use DrawDefaultInspector() to draw component’s properties. In the window, we don’t have such luxury, but we have OnGUI() which works in the same way.
Example
To demonstrate it, let’s create a simple window that we can open in the Editor.
Again like in the previous post we need to create script inside Editor folder. This code will be stripped when you will make a build of your project.
Now it’s time to code!
using UnityEngine; using UnityEditor; /// <summary> /// Custom window class that is displaying sample editor window. /// </summary> public class CustomWindow : EditorWindow { // Add menu named "Custom Window" to the menu. [MenuItem("Window/Custom Window")] /// <summary> /// Method called to show window. /// </summary> public static void ShowWindow() { // Get existing open window or if none, make a new one: var window = GetWindow(typeof(CustomWindow)); window.Show(); } /// <summary> /// Unity method that renders editor window. /// </summary> private void OnGUI() { EditorGUILayout.LabelField("Simple Custom Window", EditorStyles.boldLabel); } }
That wasn’t hard to do. ?
Now let’s add some variables and let’s edit them!
using UnityEngine; using UnityEditor; /// <summary> /// Custom window class that is displaying sample editor window. /// </summary> public class CustomWindow : EditorWindow { // Sample variables that are used to display editing in the window. private bool checkbox; private float number; private string textSample; // ... /// <summary> /// Unity method that renders editor window. /// </summary> private void OnGUI() { // ... // Sample variables displayed in the window. checkbox = EditorGUILayout.Toggle("Sample checkbox", checkbox); number = EditorGUILayout.Slider("Sample slider", number, 0, 10); textSample = EditorGUILayout.TextField("Sample text", textSample); } }
That’s nothing to be afraid of! ?
So let’s add button to our window!
using UnityEngine; using UnityEditor; /// <summary> /// Custom window class that is displaying sample editor window. /// </summary> public class CustomWindow : EditorWindow { // ... // Click count on the button. private int clickCount; // ... /// <summary> /// Unity method that renders editor window. /// </summary> private void OnGUI() { // ... // Displaying button with click count. if (GUILayout.Button(string.Format("You've clicked me {0} times!", clickCount))) { clickCount++; } } }
Basically, from this point, there is just one more thing to know about. In my examples, I’m using EditorGUILayout and GUILayout classes, which are making creating layouts painless. It’s enough for most cases, but sometimes you might want to build exactly to your crazy needs. In that case, you could want to use regular EditorGUI or GUI classes, but there is one catch. You would need to handle the positioning of your elements yourself. Some people like one way, others like the other way, it boils down to your preferences.
If you want to take a closer look at the project, you can do it on my public repository. ?
All the best, and keep on programming! ??