Game Development Reference
In-Depth Information
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(CameraLookAt))]
public class CameraTargetEditor : Editor
{
public override void OnInspectorGUI()
{
CameraLookAt targetScript = (CameraLookAt)target;
targetScript.cameraTarget =
EditorGUILayout.Vector3Field ("Look At Point",
targetScript.cameraTarget);
if (GUI.changed)
EditorUtility.SetDirty(target);
}
}
This script doesn't do much yet; we now have a
Vector3
handle in our script that dis-
plays the position of the camera's target (the specific point it is looking at). What is very
nice here is that you can edit the values and the camera will automatically transform itself
to look at the new point. To demonstrate this, create a new scene named
EditorDemos
in
Assets\Scenes
and attach the
CameraLookAt
script to
Main Camera
. If you
then select the
Main Camera
game object in the hierarchy, you will see the following set-
tings in the
Inspector
pane:
This is a lot easier than messing with the rotation values of the ordinary camera. Let's con-
tinue to add more functionalities that will blow your mind.
Note
If the custom editor script depends on certain properties or components being available on
the game object you attach it to, then be sure to use the
RequireConponent
attribute
on the base class (not the
CustomEditor
script).