This is the 24th day of my participation in the August Text Challenge.More challenges in August

An overview of the

The previous two articles covered drawing controls and Windows in the editor in Unity. There is another important area in Unity’s editor area, and that is the Scene view, commonly known as the Scene. If you want to extend the Scene view, it must be based on a base-object. In other words, if you want to display the content of an extension in the Scene view, you must select a game object in the current Scene view in the Hierarchy view. In the Hierarchy view, different game objects can have different Scene effects. So let’s see

Function implementation

To get an object, you first need to make some marks on the object. First, create an empty script and attach it to the object. Here a Cube is created and an empty script is mounted

Next we’ll start writing a script to edit the game object in the Scene view. This script must also inherit from the Editor and reference the namespace using UnityEditor; And the script should be placed in the Editor folder.

using UnityEditor;

[CustomEditor(typeof(SceneTest))]
public class SceneEditor : Editor
{
    
}
Copy the code

Next draw in OnGUI, and you can create your own desired functionality. But only if we get the object of the test script

SceneTest test = (SceneTest)target;
Copy the code

Draw a text box over an object to display its position in the format of object name: Object position. Here to draw text box using the Handles. The Label, and an article on the use of EditorGUILauout LabelField is different, one is in the Scene view, one is in the window.

// Draw the text box Handles.Label(test.transform.position + vector3. up * 2, test.transform.name + ":" + test.transform.position.ToString());Copy the code

Results the following

Next, start drawing the UGUI display information

The preferred way to start a GUI drawing is BeginGUI->BeginArea->EndArea->EndGUI.

 Handles.BeginGUI();
Copy the code

Then divide a display area for the GUI we want to draw

 GUILayout.BeginArea(new Rect(100, 100, 200, 200));
Copy the code

The area position is all set, the next is to implement the function, first to a button

// Draw Button if (guilayout.button (" test Button ")) {debug.log (" Click test Button in Scene view!" ); }Copy the code

One more text box, note that this is not the same as the Handles.Label text box, so don’t confuse it

// Draw the textbox guilayout. Label(" Textbox in the Scene view!" );Copy the code

After drawing, remember to close the Area and GUI

// Finish drawing guilayout.endArea (); Handles.EndGUI();Copy the code

This is basically the end of all editor extensions, basically there are three: control rendering, window rendering, Scene view extension.

Results show

Let’s see what the final result is

Source code address

GitHub download address: Click here to skip to download

Write in the last

Expand Uniy introduced basically just a matter of, in the process USES a lot of custom features, and provide some of the Unity editor interface, hope can through the article, to deepen your understanding of the customization features as well as the editor to expand, hope to be able to help you, also hope you can more convenient to use the Unity editor. If there is something wrong in the article, you are welcome to point it out.