"Unity" 8.5 extension editor

Source: Internet
Author: User

Categories: Unity, C #, VS2015

Date Created: 2016-04-27 I. INTRODUCTION

You can create your own custom design tools in Unity through editor windows. Scripts from Editorwindow (rather than monobehaviour) can affect Gui/guilayout and editorgui/editorguilayout controls. Alternatively, you can use the custom viewer (named Inspectors) to display the GUI controls in the Game Object Viewer (Gameobject Inspector). Ii. Custom Editor window

You can create any number of custom windows in your application. These windows and viewers (Inspector), scenes, and other built-in Windows function similarly. This is ideal for adding a user interface to a game's subsystem.

The creation of a custom editing window (editor windows) consists of several steps:

1. Create a script from Editorwindow.

2. Use the Code trigger window to display automatically.

3. Execute the GUI code of the tool.

1. editor window (Editorwindow)

In order to make the editor window, the script must be stored in a folder called "Editor". Write a class in this script that originates from the editor window (Editorwindow). Then, write the GUI control in the internal Ongui function.

Here is the JavaScript code sample (which can also be implemented in C #):

Class Mywindow extends Editorwindow {

function Ongui () {

The actual window code here

}

}

The Mywindow.js file is placed in a folder named "Editor" in the project.

2. Display window

In order to display the window on the screen, you need to make a menu item for display. This can be done by creating a function that can be activated by a menu item (MenuItem) property.

The default behavior in Unity is to reuse windows. Therefore, selecting the menu item again will display the existing window. You can do this by using the Editorwindow.getwindow function, which is the JavaScript code example (you can, of course, be implemented in C # instead):

Class Mywindow extends Editorwindow {

@MenuItem ("window/my window")

static function ShowWindow () {

Editorwindow.getwindow (Mywindow);

}

function Ongui () {

The actual window code here

}

}

This creates a standard, dockable editor window that stores its location between calls, and can be used in custom layouts ...... Wait a minute. If you need more control over the windows you create, you can use Getwindowwithrect.

3, the Implementation window GUI

The actual content of the window is rendered by executing the Ongui function. You can use the Unitygui class (GUI and guilayout) used in the game GUI. In addition to this, unity also provides some GUI controls in the editor-only class Editorgui and Editorguilayout. These classes are added to the controls that are already available in the General class, so you can mix and match as you like.

The following C # code shows how you can add GUI elements to your custom editor window:

usingUnityeditor;usingUnityengine; Public classmywindow:editorwindow{stringMyString ="Hello World"; BOOLgroupenabled; BOOLMybool =true; floatMyfloat =1.23f; //Add a menu item named "My window" to the Window menu[MenuItem ("window/my Window")]     Public Static voidShowWindow () {//Show an existing window instance (if it doesn't exist, create one first)Editorwindow.getwindow (typeof(Mywindow)); }    voidOngui () {Guilayout.label ("Base Settings", Editorstyles.boldlabel); MyString= Editorguilayout.textfield ("Text Field", myString); Groupenabled= Editorguilayout.begintogglegroup ("Optional Settings", groupenabled); Mybool= Editorguilayout.toggle ("Toggle", Mybool); Myfloat= Editorguilayout.slider ("Slider", Myfloat,-3,3);    Editorguilayout.endtogglegroup (); }}

This example produces a window that looks like this: three, custom viewer

The key to increasing game creation speed is to create custom viewers for common components. For the convenience of example, we will use a very simple script that keeps the object looking at a point at all times.

The following is an example of lookatpoint.js:

var lookatpoint = Vector3.zero;

function Update () {

Transform. LookAt (Lookatpoint);

}

This preserves the object toward a point in the world coordinate space. Let's make it a little more cool!

The first step in making the editor run well is to have the script run even when you are not testing the game. We do this by adding the Executeineditmode property to it:

@script Executeineditmode ()

var lookatpoint = Vector3.zero;

function Update () {

Transform. LookAt (Lookatpoint);

}

Try adding the script to your main camera and dragging it around in scene view.

1. Make a custom editor

That's fine, but we can make it more beautiful by customizing the viewer. To do this, we need to create an editor for it.

The following is a JavaScript file created in the Editor folder called Lookatpointeditor:

@CustomEditor (Lookatpoint)

Class Lookatpointeditor extends Editor {

function Oninspectorgui () {

Target.lookatpoint = Editorguilayout.vector3field ("Look at point", target.lookatpoint);

if (gui.changed)

Editorutility.setdirty (target);

}

}

Note that the class must inherit from editor. Where, @CustomEditor property reports the editor of Unity which component it will act as.

Whenever Unity displays the viewer, the code in the Oninspectorgui will be executed. So you can put any GUI code here-it runs like the game's Ongui (), but it runs inside the viewer. The editor defines the target properties that can be used to access the object being inspected.

When a user changes any value by checking gui.changed, the Editorutility.setdirty code is executed.

In this case, we will use one of the vector 3 (Vector3) fields for the Transform viewer (Transform Inspector)-as follows:

There's still a lot of work to do, but we're done here-the play is still back ...

2. Scene View add

You can add additional code to a scene view by executing Onscenegui () in the custom editor. In this case, a second position control handle is added, allowing the user to drag the viewing target point in the scene view. The following is a sample JavaScript code:

@CustomEditor (Lookatpoint)

Class Lookatpointeditor extends Editor {

function Oninspectorgui () {

Target.lookatpoint = Editorguilayout.vector3field ("Look at point", target.lookatpoint);

if (gui.changed)

Editorutility.setdirty (target);

}

function Onscenegui () {

Target.lookatpoint = Handles.positionhandle (Target.lookatpoint, quaternion.identity);

if (gui.changed)

Editorutility.setdirty (target);

}

}

The Onscenegui runs the same way as the Oninspectorgui-except that the former is run in the scene view. To make the editing interface easier, you can use functions defined in the handle (Handles) class. All of these functions are designed for a three-dimensional scene (3D scene) view. If you want to place a two-dimensional GUI object (GUI, Editorgui), you need to include these objects in calls to Handles.begingui () and Handles.endgui ().

"Unity" 8.5 extension editor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.