Game Development Reference
In-Depth Information
Custom editors
Say you want to control the entire scope of a single class or
ScriptableObject
; this
is where
CustomEditor
scripts come in.
They can be used against any script that can be attached to a game object to alter how it
works in the Unity editor inspector.
As an example of these (the best way to show custom editors is through code), we will
add some functionality to a camera to provide us with better control over it in a scene.
First, we'll need a very simple camera script that will point the camera to a specified tar-
get, starting at
0
,
0
,
0
. So, create a new script named
CameraLookAt
in
As-
sets\Scripts
and replace its contents with the following code:
using UnityEngine;
public class CameraLookAt : MonoBehaviour
{
public Vector3 cameraTarget = Vector3.zero;
void Update()
{
transform.LookAt(cameraTarget);
}
}
We can then define a
CustomEditor
script that will be run by the editor whenever it
detects a game object with the script attached to it.
Note
As with a lot of editor features, remember (as a good rule of thumb) that if a class requires
the
UnityEditor
namespace, it will need to live in the special
Editor
folder in your
project.
So, create a new C# script called
CameraTargetEditor
in
As-
sets\Scripts\Editor
in your project and replace its contents with the following
code: