In Unity we can create a custom inspector to control
which data from our scripts is visible
and how it is represented in the editor.
That ability to extend the editor in this way
gives us a lot of power and flexibility with our scripts
and the resulting workflow.
Let's say we had a levelling system
where every time a character earned 750 experience points
they gained a new level.
This is easy enough to calculate in the beginning
but it could be a bit cumbersome as characters
get to higher levels.
Luckily we can create a custom
inspector to make this much simpler.
Here we have a script called LevelScript.
This script represents the experience based
levelling system mentioned before.
In the script we first have a variable named experience
which will contain the total amount of experience
a character has accumulated.
Next we have a property named Level.
We can see that this property takes the current
experience value and returns the level equivalent.
For more information on properties
see the lesson on properties linked below.
In Unity we have an empty game object
with the LevelScript attached.
Notice how we can see the experience
property in the inspector.
It is easy to calculate the level when our experience is 0.
But what if it was 49865?
Let's customise the inspector to show
the level for us.
In order to customise the inspector we need
to make a folder to place our editor scripts in.
Unity requires that this folder is named
Editor.
Inside the editor folder we will create a new script.
This script will contain our custom inspector.
It can have any name,
but for the sake of this example we will
call it LevelScriptEditor.
In the LevelScriptEditor
we can begin writing our custom inspector.
Before we begin writing any editor specific code
we must first declare that we are using the
UnityEditor namespace.
For more information on namespaces
see the lesson on namespaces linked below.
Next we need to inherit from editor
instead of monobehaviour.
This in effect is how we extend the editor.
For more information on inheritance
see the lesson on inheritance linked below.
>From here we need to give our custom
inspector an attribute to tell it which
script we are customising.
The syntax for doing this starts with a
open square bracket followed by the
keyword CustomEditor and parenthesis.
Inside the parenthesis we use the keyword
typeOf to specify the type.
And then we put the name of our script
in another set of parenthesis.
We can see that in this example
the script we are customising is the LevelScript.
We end the line with a closed square bracket.
Inside our LevelScriptEditor class
we create the method public override void
OnInspectorGUI
inspector is drawn inside Unity.
For more information on what the override
keyword is doing
see the lesson on overriding linked below.
At this point the inspector for the
LevelScript has been modified.
If we go back in to Unity we can see
that we no longer see any properties of
the script in the inspector view.
Since this is not very useful
let's continue writing our custom inspector.
Back in the LevelScriptEditor
we will want to create some fields for the inspector.
We automatically have a reference to our LevelScript object
named target inside the OnInspectorGUI method.
This isn't useable until we cast it in to
an appropriately typed variable however.
To do this we will create a
LevelScript variable named myLevelScript
and set it equal to the inherited variable target.
Finally we will cast the variable
target as a LevelScript variable
by writing LevelScript in parenthesis next to it.
We want our inspector to show two pieces of information.
The amount of experience the character has,
and the current level of the character.
We can create an integer field in the inspector
using the EditorGUILayout class.
The syntax for this starts with the classname
EditorGUILayout, followed by a period
and then the method IntField
followed by parenthesis.
Inside the parenthesis we specify the name of the field
and the value of the field.
In this case we want the field to be named Experience
and the value to be the value of the experience variable
of our myLevelScript.
Since we want this field to be able to write back
to our Experience variable we set the entire line
equal to myLevelScript.experience.
We follow a similar process for the Level field.
This time however we will be using
the LabelField method of the EditorGUILayout class.
A label is a read-only control
so we do not have to worry about the user
attempting to manually adjust the level in the inspector.
Inside the parenthesis of the LabelField method
we parse in the name Level
and the value myLevelScript.Level.
Since a label control requires a string as a value
we will call the ToString method
of the Level property to turn it
in to a string.
At this point our custom inspector is complete.
We can go back in to Unity to see the result.
In the inspector we can see that
when our experience is 49865
our level is 66.
Furthermore we can see that as
we adjust the experience value
the inspector automatically tells us the level.
By customising the editor for our scripts
we can simplify and automate
many of our otherwise cumbersome tasks.
Subtitles by the Amara.org community