CREATING EDITORS IN UNITY (PART 2)
In the second part of the tutorial on creating editors in Unity we will look at the editor’s windows and drawing additional information in Scene.
1. Creating an editor window
Let’s start by creating an editor window script. First, we have to create a C # script and save it in the “Editor” directory. This script must inherit from the EditorWindow class.
The script also has the Init method that opens the window. An additional MenuItem attribute allows you to invoke a window from the Unity menu. In this case, the call is placed in the Window menu.
At this moment, our editor window is empty. We’ll add a line to the object in a moment.
To do this we need to add a list of objects to the line, the distance between the objects, the place where the object was set, and the direction in which it was set.
The method of setting objects is described in details in the first part of the tutorial.
After adding the fields to the script, we notice that, unlike the monobehavoiur scripts viewer, the fields are not displayed in the unity editor, and the window remains blank.
To add boxes to the window we have to write our own GUI based on the legacy GUI Unity. We create the OnGUI method, which will display the contents of our window.
In the method we first display the text “Settings” using the GUILayout.Label method, we use GUI legacy drawing methods here. We then display and retrieve the values of the individual fields using field-dependent methods. For Vector3 this is the EditorGUILayout.Vector3Field method, for the EditorGUILayout.FloatField float, we use GUI legacy drawing methods exclusively for editors.
Currently, our editor window looks like this:
We have added a list of objects to set in a line. Unfortunately Unity does not have a method to draw letters in the editor, we need to write this mechanism ourselves. We will start by adding two additional fields to the script that will be needed in the list creation.
The objectsListCollapsed field shows whether the list is collapsed or visible, listSizeCollections is the number of objects in the list. With these two fields we can draw the list itself:
The EditorGUILayout.Foldout method gives you the ability to hide and show elements. If set to true we start drawing the list itself. EditorGUI.indentLevel ++ This method moves all gui elements one level to the right. EditorGUILayout.IntField shows a TextBox with a list item number. If we increase the number of items, the empty items will be cast to the list, but as we reduce the number of items, items from the list will be removed starting with the last item.
After listing the number of items in the list, we draw in a loop for a list of items using the EditorGUILayout.ObjectField method.
Our window now looks like this:
We still need a button to set objects in a line. We will add it by using the GUILayout.Button method:
Now let’s add some objects to the scene:
Then assign them to the list in our editor window.
When you click the Set Object in line button, our objects will line up.
An important element of creating an editor window is the User Experience. Add some information to our window to make it easier for the user to use. Currently, our window script looks like this:
We will start by adding information for the user that the list of objects must have more than 0 elements before they can be placed. To do this we will add a condition before showing the Set Objects in line button to check the number of list items when the number is 0 displayed using the EditorGUILayout.HelpBox method.
Then we will see if the list is empty. To do this we will add the bool emptyElement variable to the OnGUI method, and in the loop that draws the list of objects, we will check whether the object is not null.
The last important information is to check if the forward field is not zero so that you can position the items in the correct direction.
We have already added information for the user in the editor window, now we will add some additional information in the scene window. To do so we have to hook up to the SceneView.onSceneGUIDelegate delegate – it is best to do this during onfocus and ondisable events. We will now set the OnSceneGUI method, which will be responsible for drawing in the Scene window.
Our OnSceneGUI method currently draws in the loop the position of objects that we added to our list. To do this, we use the Handles class method, which is used to draw elements in the Scene window – these elements can be the simple GUIs as they are currently in our Label method, but they can also serves as controls to move objects.
Let’s now add the possibility to change the startPosition field of our editor window.
The Handles.PositionHandle method draws on the item of our vector the element for moving objects, while Handles.DotCapi shows the position of the startPosition square for magenta colors so we can better see where the startPosition is.
When we move the position of Handle, the position of our startPosition in the editor window changes. The last thing we’ll add is the display of the direction in which the objects will be aligned. We use the Handles.ArrowCap method, which draws the arrow in a given direction.
At this point we will finish the current tutorial on creating editors in Unity. The entire code of our window looks like this:
In this tutorial you have learned how to create editors in the Unity. In the first part we used ExecuteInEditMode and wrote our own editorial inspectors. But in this part we have created an editor window, added messages to the user, and the ability to control the window from the Scenes window using Handles.