This is the 23rd day of my participation in the August More Text Challenge.More challenges in August

An overview of the

In the last article, we talked about how the Inspector panel has been beautified with the Unity editor extension. The Unity editor extension has a lot more functionality than just drawing controls for the Unity editor, creating a new window for the editor, and setting its layout. Let’s learn how to create a custom layout window in Unity editor.

Function implementation

To create a window, the script must inherit from the EditorWindow. When the script inherits from the EditorWindow, the script must reference using UnityEditor; Namespace, otherwise inheritance is wrong. First we create an inherited EditorWindow script, again in the Editor folder

using UnityEditor;

public class EditorWindowTest : EditorWindow
{ 
}
Copy the code

We create a static method to open the window, and in this method we can customize the design of the window.

static void OpenWindow()
{
}
Copy the code

To display this method in the Unity editor, we need to use another Unity feature [MenuItem] instead of just inheriting the Editorwindow.

Let’s take a look at MenuItem. MenuItem is the UnityEditor property. It is an extension to the editor and has two main functions:

  1. Adding menu items to the main menu, in the following editor section, is an important use that is often used when developing plug-ins.
  2. Adds a menu item in the context of a component in the properties panel to extend an existing component in the editor

Note: Functions are generally private and must be static to use the MenuItem attribute. Why is it necessary to be static? Because the class can be instantiated without inheriting from MonoBehavior. For example, adding a reset property function to the rigidbody does not require the addition of script components, so we can concentrate many similar menu items in a single extension script. So in the above code we use static. It’s commonly used

    public MenuItem(string itemName);
Copy the code

ItemName is the menu path name, menu shortcut keys can be added (space followed by the following symbol Settings), as follows:

_w Single shortcut key W # W Shift + W % W CTRL + W & W Alt+ WCopy the code

We’ll leave MenuItem here for now, and then go back to setting a path to our methods, such as OpenWindow under the menu bar Tools

[MenuItem("Tools/OpenWindow")]
static void OpenWindow()
{
}
Copy the code

So now the menu bar has the method that we just wroteNext we will create a window, first specify the size of the window

Rect rect = new Rect(0, 0, 500, 500);
Copy the code

Once the size is set, all that is left is to create the form

EditorWindowTest window = (EditorWindowTest)EditorWindow.GetWindowWithRect(typeof(EditorWindowTest), rect, true, "Custom create window "); window.Show();Copy the code

When you’re done, click on the menu bar and you’ll see something like this

Window here we will create a success, of course, this is just a window into the book, what all have no, let’s together to enrich this window, add input box to the window, to add a text box shows the mouse position, add a selection map function, opening and closing notification bar, close the window, and other functions. These functions are written in the OnGUI method

Add an input box control

Text = EditorGUILayout. TextField (" please enter the content: ", the text).Copy the code

Open the notification

Start by creating a button that turns on notifications

Guilayout. Button(" Open notification ", guilayout.width (200))Copy the code

Then write the method to open the notification bar

This.ShowNotification(new GUIContent(" this is a notification message "));Copy the code

Results the following Close to inform

Close notification and the above method is basically the same, first create a button in the write method

If (guilayout.button (" close notification ", guilayout.width (200))) {// Close notification this.removenotification (); }Copy the code

Add text box to display mouse position

Method of obtaining mouse position (disadvantage, not available everywhere)

Event.current.mousePosition
Copy the code

Add a text box and display the mouse position

EditorGUILayout. LabelField (" mouse position: ", the Event. Current. MousePosition. The ToString ());Copy the code

Choose map

The function of selecting texture is basically the same as in the previous article

Texture = EditorGUILayout. ObjectField (" select map ", texture, typeof (texture), true) as texture;Copy the code

The overall effect of the above functions is as follows

Knowledge expansion

The following writes the lifecycle that the extension editor might use when the window gets focus

Private void OnFocus() {debug.log (" Call once when window gets focus "); }Copy the code

When a window loses focus

Private void OnLostFocus() {debug.log (" Called once when window loses focus "); }Copy the code

When any object in the Hierarchy view changes

Private void OnHierarchyChange() {debug.log (" called once when any object in the Hierarchy view changes "); }Copy the code

When any object in the Project view changes

Private void OnProjectChange() {debug.log (" Called once when any object in the Project view changes "); }Copy the code

Hot update of window panel

Private void OnInspectorUpdate() {debug.log (" Window panel hot update "); private void OnInspectorUpdate() {debug.log (" window panel hot update "); This.repaint (); }Copy the code

When the window is open and a game object is selected in the Hierarchy view

Private void OnSelectionChange() {// When the window is open, Using foreach (var item in selection.Transforms) {// Optionally optionally, this turns on a loop debug.log ("OnSelectionChange: " + item.name); }}Copy the code

When the window closes

Private void OnDestroy() {debug.log (" Called when window is closed "); }Copy the code

Let’s take a look at the final result

The source address

GitHub download address: Click here to skip to download

Write in the last

This article is mainly introduced, Unity editor how to carry out window extension, in order to be more convenient in the development process, the next article will continue to introduce in Unity how to carry on the extension of the editor, the function is also very practical, welcome to learn and exchange.