Custom Editors
The key to accelerating the game production process is to create a custom editor for the frequently used components, and for example, we'll use this extremely simple script to explain that it always keeps an object looking at a certain point.
Public class Lookatpoint:monobehaviour { public Vector3 lookatpoint = vector3.zero; void Update () { transform. LookAt (Lookatpoint); }}
This script will keep an object facing a certain point in the world space, let's do a bit more cool!
The first step is to make it work in the editor: Even if you don't have a test game, you can let the script run. We add the Executeineditmode attribute to it to do this.
[Executeineditmode] Public class Lookatpoint:monobehaviour { public Vector3 lookatpoint = vector3.zero; void Update () { transform. LookAt (Lookatpoint); }}
You can add this script to the main camera and then drag the camera to test it in scene view.
Making a Custom Editor
Test discovery works fine, but we can customize an editor for it to make it more nice. To do this, we need to create an editor for it, and create a lookatpointeditor C # script in the folder named "Editor".
usingUnityengine;usingUnityeditor;usingSystem.Collections; [Customeditor (typeof(Lookatpoint))] Public classLookatpointeditor:editor { Public voidOninspectorgui () {Lookatpoint lap=(lookatpoint) target; Lap.lookatpoint= Editorguilayout.vector3field (" Look at point", Lap.lookatpoint); if(gui.changed) editorutility.setdirty (lap); }}
This class must inherit the editor class. The Customeditor property tells unity which component needs to behave as an editor
The code in the Oninspectorgui method executes when unity displays the editor in Inspcetor. You can put any GUI code here-its work is similar to the Ongui method in the game, except that it is executed in Inpector, and editor defines the target property so that you can get the object being viewed.
By checking the gui.changed, the Editorutility.setdirty code executes if the user is found to have modified any value. The effect is to mark the specified object as dirty.
[Unity uses the dirty tag internally to determine when the resource is modified and needs to be saved.] For example, if you modify a prefab monobehaviour or scriptableobject variable, you must tell unity that the value is changed. Unity's built-in component, which automatically calls the SetDirty method when a property changes, does not do this automatically, like Monobehaviour or scriptableobject, so if you want the value of the change to be stored, You have to call the SetDirty method]
In this example, we created a 3-D vector edit box similar to the one in the transform editor, such as:
There's a lot more to do here, but so far we're going to catch bigger fish ... ^~^
Scene View Additions
You can implement the Onscenegui method in your custom editor to add extra code to the scene view. In this example, we will add a 2nd position processor so that the user can drag and drop the gaze point in the scene view.
usingUnityengine;usingUnityeditor;usingSystem.Collections; [Customeditor (typeof(Lookatpoint))] Public classLookatpointeditor:editor { Public voidOninspectorgui () {Lookatpoint lap=(lookatpoint) target; Lap.lookatpoint= Editorguilayout.vector3field (" Look at point", Lap.lookatpoint); if(gui.changed) editorutility.setdirty (lap); } Public voidOnscenegui () {Lookatpoint lap=(lookatpoint) target; Lap.lookatpoint=Handles.positionhandle (Lap.lookatpoint, quaternion.identity); if(gui.changed) editorutility.setdirty (lap); }//I didn't see any effect, and I didn't know much about it.}
Onscenegui works just like the Oninspectorgui method-except that it runs in the scene view. To help you make the editing interface, you can use the methods defined in the Handles class. All of the methods defined inside are designed to be in 3D Scene
View in the work design.
If you want to place 2D GUI objects (Gui,editorgui or other), you need to wrap them in Handles.begingui () and Handles.endgui ().
Unity Extension Editor-type 3:custom Editors