Unity EditorWindow 101

One of the really nice things with Unity, among others, is the ability to create custom editor windows. Those can be used for a lot of things from little useful debug panels to full fledged editors for your game.

To create an editor window we just create a class extending EditorWindow.
EditorWindow is in the UnityEditor namespace so don’t forget to use it.

using UnityEditor;
using UnityEngine;
using System.Collections;

public class MyEditorWindow : EditorWindow {
}

The first method we’ll need is a static function to create the window.
You can give this method the name you want. Let’s call it ShowWindow().

using UnityEditor;
using UnityEngine;
using System.Collections;

public class MyEditorWindow : EditorWindow {

  [MenuItem("Window/My Editor")]
  static void ShowWindow(){
    EditorWindow.GetWindow<MyEditorWindow>( "My Editor" );
  }

}

This ShowWindow() method is preceded by a MenuItem  attribute. This is used to specify where to add a menu item that will execute this function.
By giving it the string “Window/My Editor” we will have a “My Editor” item under the “Window” menu. You can specify the hierarchy of the menu item to have sub menus with a string like “Window/My Editor/Editor 1”.
The custom editors don’t have to be in the “Window” menu, if you set the string to “My Editor/Custom Editor”, this will add a new menu entry “My Editor” in the Unity toolbar.

Inside this method we call EditorWindow.GetWindow<T>( … ) to get or create the window. As any Generic method, it need to be given the type of window we want, here “MyEditorWindow”. The other parameter is not required but can be used to specify the title of the window.

We can now display our magnificent default empty editor window.

For now we will specify a minimum size for this window.
EditorWindow already has a Vector2 to takes care of this : minSize.

using UnityEditor;
using UnityEngine;
using System.Collections;

public class MyEditorWindow : EditorWindow {

  [MenuItem("Window/My Editor")]
  static void ShowWindow(){
    MyEditorWindow window = EditorWindow.GetWindow<MyEditorWindow>( "My Editor" );
    window.minSize = new Vector2 (500, 200);
  }

}

First we keep a reference to the window created by GetWindow.
We can then assign a new Vector2 to window.minSize in order to set the minimum window size. 

Note that because this is done when the window is created this method needs to be called again for the changes to take effect.
Closing and reopening the editor window will do the trick.


Because a plain empty window is not that useful we will Implement the OnGUI() method, where all the magic happend.

using UnityEditor;
using UnityEngine;
using System.Collections;

public class MyEditorWindow : EditorWindow {

  [MenuItem("Window/My Editor")]
  static void ShowWindow(){
    MyEditorWindow window = EditorWindow.GetWindow<MyEditorWindow>( "My Editor" );
    window.minSize = new Vector2 (500, 200);
  }

  void OnGUI(){
  }

}

Let’s add a label and a button to our window.

using UnityEditor;
using UnityEngine;
using System.Collections;

public class MyEditorWindow : EditorWindow {

  [MenuItem("Window/My Editor")]
  static void ShowWindow(){
    MyEditorWindow window = EditorWindow.GetWindow<MyEditorWindow>( "My Editor" );
    window.minSize = new Vector2 (500, 200);
  }

  void OnGUI(){
    GUILayout.Label ("Hello world!");
    if ( GUILayout.Button("Click me") ) {
      Debug.Log ("clicked");
    }
  }

}

Here, static methods from “GUILayout” are used to create the label and the button.
There are multiple ways to create GUI elements, using “GUILayout” let the underlying layout deal with their size and position.
An editor window has a default vertical layout so each elements are placed one under the other and occupy all available width space.

The Button method returns true when the button is clicked so we can use it inside a if() statement to execute code when it occurs.
A simple Debug.Log(…) let us see that everything works nicely.


Our window now contains a label and a button.

That’s it, we have successfully created our first editor window.
It doesn’t really do anything for now but it’s a first step toward creating useful editors.