top of page

001-Introduction

1.1 Introduction

Unity Editors are used to draw fields such as string,int and list of collections into the editor. Along with that they can be used in adding additional functionalities by the use of buttons,custom drawing etc. In this tutorial we are going to be discussing broadly about two kinds of editors that is 

1. Custom Editors

2. Editor Windows

1.2 Basic Editors

Open the 001 Folder in the project file and open up the ball script. In this script we can see that there are 3 fields that is 

1. public string ballName

2. public int ballRadius

3. public float ballDensity

All these fields are getting drawn in the inspector by unity's editor function

​

image.png
image.png
1.2 Custom Editors

Custom editors allow us to control the way editors are drawn for the monobehaviour. Let us now build our first custom editor for the ball monobehaviour that allows us to control how it is drawn. 
To make a custom inspector keep two things in mind:

1. They must inherit from Editor instead of monobehaviour

2. They must have an attribute of [CustomEditor(typeof(class to be drawn))] specifying class for which they are used.

1.3 Ball Custom Editor

1. Let us build our first custom editor for the ball class and call it as BallEditor(Having editor at the end is optional and I am using this for clarity). 

2. To make sure that this is a custom editor make sure it inherits from Editor Class and has [CustomEditor(typeof(Ball))] set in the attribute at the top.

3. Now let us take a look at inspector and we will see tha the ball in the editor still looks the same.

image.png
image.png
1.4 OnInspectorGUI

Inspector in Unity is drawn in OnInspectorGUII() function. As we have inheritted from the Editor class for the ball script. We can override the OnInspectorGUI() method.

Let us add OnInspectorGUI() method to the script

image.png
image.png

In the override OnInspectorGUI() we have added base.OnInspectorGUI() as it allows us to draw the inspector in default way it is supposed to be drawn.

​

image.png
image.png

Adding base.OnInspectorGUI() twice makes the default editor to be drawn twice

image.png
image.png

Removing base.OnInspectorGUI() prevents the editor from being drawn at all

1.5 EditorWindow

EditorWindow is a functionality that allows us to open editor windows for to implement different functionalities for which custom editors are not suited and when we want to make functionalities independent of the game objects.

Some good examples are :

1. Frame Debugger
2. Lighting Window

etc

image.png
1.5 Making our first editor window

1. To make an editor window make a class and make it inherit from the EditorWindow class that can be included with using UnityEditor namespace.

2. Let us make our own editor window and call it as MyFirstEditor and making it inherit from the EditorWindow class.

image.png

1. To open the editor window we are going to create a static method called windowOpener in which we are going to add GetWindow<className> method that opens up the editor window.

2. Menu Item Attribute allows us to execute the function from the topmost menu with the address specified in the paranthesis.

3. Clicking on the button opens up the window and as we can see the editor window is currently blank as we have written no editor code in it.

image.png
image.png
End

Kindly support me through my Patreon account if you find these tutorials useful. Feel free to connect with me on Twitter. This support enables me to create more educational content.

bottom of page