Unity3d Editor Plugin Authoring Tutorial

Source: Internet
Author: User
Tags class definition

How to get the editor to run your code how to get the editor to run your codeUnity3d can execute your editor code through event triggering, but we need some compiler parameters to tell the compiler when it needs to trigger the code. [MenuItem (XXX)] declares above a function, tells the compiler to add a menu item to the Unity3d editor, and calls the function when it clicks the menu item. Trigger function can write any legitimate code, can be a resource batch program, you can also pop up an editor window. The code can access the currently selected content (through the selection Class) and determine the display view accordingly. Similarly, [ContextMenu ("XXX")] can add a menu item to your context menu. When you write some component script, when it is attached to a gameobject and you want to see the effect in scene view in Edit view, you can write [Executeineditmode] above the class to inform the compiler. Functions such as Ongui and update of this class are also called in edit mode. We can also use [Addcomponentmenu ("xxx/xxx")] to associate the script with the Component menu and click on the menu item to add the component script for Gameobject. start writing an editorTo avoid unnecessary inclusions, the Unity3d Runtime and editor classes are stored in different assemblies (Unityengine and Unityeditor). Before you start writing the editor, you need a using Unityeditor to import the editor's namespace. Some of the code might be executed by both the runtime and the editor, and if you want to differentiate between them, you can use the # if Unity_editor ... #endif宏来对编辑器代码做特殊处理. Before you start to actually write the code, I think you need to know that all the scripts placed in the directory named editor will be compiled after the other scripts, which makes it easier for you to use those runtime content. The scripts in those directories are not accessible to the contents of the editor directory. So, you'd better write your editor script in the editor directory. How to create a custom editor windowCreate your own windowIf you want to customize an editable panel, then you need to write a class that inherits from Editorwindow. Typically, you also need to write a [MenuItem] to tell the compiler when to open the Panel. The callback for this event should be a static method and return an instance of a window. now, when you click on the corresponding menu item, a blank window pops up. And you can drag and dock as you like the Unity3d Editor's prefabricated window. Let's see how we can implement the features we want in the window. Expand your window As with the runtime GUI, if you need to add interactive controls to the window, you must override the Ongui method. As with the runtime GUI, you can even use any plug-ins that extend from native GUI systems (such as Igui and Guix) to simplify your plug-in development process (only initial testing, deeper usability is yet to be verified). At the same time, the editorguilayout under the Unityeditor namespace provides some more convenient interfaces and controls on the native GUI, allowing you to easily use some of the editor-specific UI controls. In addition to Ongui, you may also need some of the following callbacks to trigger some specific logic (see the official documentation for the full list): Onselectionchange, but when you click on an item, it triggers the onfocus/onlostfocus, gets and loses focus . Extend your window furthercustom controls are used in the same way as the run-time GUI, and if you're going to customize your own controls, the simplest way is to implement a static method (or not static) and provide some optional arguments in the method  The interior is based on these parameters to complete the layout of the control (as you did in Ongui). If you're going to implement a custom control inside a window class, you can use the partial class to better manage your code. Draw 2D content to draw a picture you can use Gui.drawtexture to complete the drawing of the picture resource. Drawing the underlying primitive GUI itself does not provide a way to draw the underlying entities, but there are several ways to encapsulate these methods. Draw segments: The mapping of a pixel with gui.drawtexture and matrix rotation to complete the line. Draw a rectangle: Use the Gui.box and style settings to encapsulate the rectangle and rectangle fill boxes. The resource selector Editorlayout.objectfield control provides a resource selection logic that requires specifying a resource type when it is generated. You can then drag that kind of resource to the control or click the small circle next to the control to make a list selection. how to store edits
you may need to create a class that inherits from Serializedobject to save the edited data. Objects that inherit from Serializedobject can be used to store data without participating in rendering, and can eventually be packaged into Assetbundle. For storage of content such as the current editing options, another Serializedobject class (related to the specific system design) may be required. Wizard-style editing windowIn many cases you may need an edit panel with a lot of parameters, and then a button to confirm it after the edit is complete. You don't have to do this yourself, Unityeditor provides scriptablewizard to help you develop quickly. He is inherited from the Editorwindow, so their use is very similar. Note, however, that onwizardcreate () is called when you click the Confirm button. In addition, Scriptablewizard.displaywizard can help you build and display the window. How to extend the Inspector panelWhen you select an object in Unity3d, the Inspector panel displays the properties of the object. We can extend the panel for a type of object, which is useful when developing plug-ins for Unity3d. define when inspector is triggered The Custom Inspector panel needs to inherit the editor class. Because the functionality is relatively specific, you do not need to define when the code is triggered, and the corresponding code executes automatically when you click on the object it corresponds to. So how do you define the type it corresponds to? This can only be done by using the compiler's command [Customeditor (typeof (XXX)]) before your class definition. accessing the object being editedin the Inspector view, we often need to access the object being edited.  This association is provided by the member variable target of the editor class. However, it is important to note that target is an object of type objects, which may require type conversions (you can use generics in C # to avoid duplicate type conversions). implement your own Inspector interfaceThe only difference between extension editor and extended Editorwindow is that you need to rewrite Oninspectorgui instead of Ongui. Also, if you want to draw the default editable items, simply call Drawdefaultinspector. defining an edit handle in the scene interfacewhen an object is selected, we may want to define some edits or displays in the scene view as well. This work can be done through the Onscenegui and handle classes. Onscenegui is used to handle events from the scene view, while the handle class is used to implement some 3D GUI controls in the scene view (for example, a position controller that controls the location of objects). Refer to the official reference documentation for specific usage. Some common feature descriptionsAssetdatabase.createasset can help you create a resource instance from the resource directory. Selection.activeobject returns the currently selected object. Editorguiutility.pingobject is used to implement an action that clicks on an item in the Project window. The editor.repaint is used to redraw all the controls on the interface. Xxximporter is used to set specific import settings for a resource (for example, in some cases you need to set the imported map to be readable). Editorutility.unloadunusedassets is used to free unused resources and prevent your plug-in from generating a memory leak. The event.use is used to mark that the event has been processed. Editorutility.setdirty is used to inform the editor that the data has been modified so that the next time the data is saved will be stored.
Unity3d Editor Plugin Authoring Tutorial

Unity3d Editor Plugin Authoring Tutorial

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.