Unity3d Attribute Summary (Turn)

Source: Internet
Author: User

For two examples, using the [Serializefiled] property on a variable, you can force the variable to be serialized and be assigned on Unity's editor.

Using the [Requirecomponent] property on class will automatically append the required component on the class's gameobject.

The following is a list of all the attribute found in the Unity website documentation, followed by a description of these attribute and a small test in order.
Some examples use the official Unity example.

Unityengineaddcomponentmenu

You can add custom items to the Unityeditor's component menu. Menus can be set to multiple levels, using a slash/divider. When you select Gameobject in hierarchy, click the menu item to append the component to the Gameobject.
For example, the following code can accomplish the effect.

[Addcomponentmenu ("Testmenu/testcomponet")]public class Testmenu:monobehaviour {}

assemblyiseditorassembly

The assembly-level attribute, which uses the class of the attribute, is considered to be editorclass. The specific usage is unclear.

ContextMenu

You can add options in the ContextMenu of Inspector.
For example, the effect of the following code

public class Testmenu:monobehaviour {    [ContextMenu ("Do Something")]    void DoSomething () {        Debug.Log ("Perf ORM operation ");}    }

Contextmenuitemattribute

This property is a new feature provided after Unity4.5, you can append a right-click menu to the variable above the inspector and execute the specified function.
Example:

public class Sample:monobehaviour {    [Contextmenuitem ("Reset", "Resetname")] public    string name = "Default"; 
   void Resetname () {        name = "Default";    }}

Disallowmultiplecomponent

Use this property for a monobehaviour subclass, so you can add at most one instance of that class on the same gameobject.
When you try to add more than one, the following prompt appears.

Executeineditmode

By default, methods such as Start,update,ongui in Monobehavior need to be executed on play.
This property allows class to execute in Editor mode (not play mode).
But there are some differences with play mode.
For example:
The Update method is called only if there are objects in the scene editor that have changed.
The Ongui method is called only when the event is received by the Gameview.

Headerattribute

This property can be added to the header of the variable in the inspector.
Example:

public class Exampleclass:monobehaviour {    [Header ("Health value")] public    int currenthp = 0;    public int maxhp = +;    [Header ("Magic Value")]    public int currentmp = 0;    public int maxmp = 0;}

Hideininspector

Using this property on a variable allows the public's variable to be hidden on the inspector, i.e. it cannot be edited in the editor.

Imageeffectopaque

For use on onrenderimage, you can make the rendering order before the transparent object, after the non-transparent object.
Example

[Imageeffectopaque]void onrenderimage (rendertexture source, rendertexture destination) {}
Imageeffecttransformstoldr

Rendering from HDR to LDR is not an exact method of use.

Multilineattribute

Used on a string type, you can enter multiple lines of text on the editor.

public class Teststring:monobehaviour {    [Multilineattribute] public    string MText;}

Notconvertedattribute

Used on a variable, you can specify that the variable is not converted to the type of the target platform when it is build.

Notflashvalidatedattribute

Used on a variable, the variable is not type checked on the Flash platform build.
This property has been removed from the Unity5.0.

Notrenamedattribute

Renaming variables and methods is forbidden.
This property has been removed from the Unity5.0.

Propertyattributerangeattribute

Used on an int or float type to limit the range of input values

public class testrange:monobehaviour{    [Range (0, +)] public int HP;}
Requirecomponent

Use on class to add a dependency on another component.
When the class is added to a gameobject, the component is automatically added if the Gameobject does not contain a dependent component.
And the componet is not removable.

Example

[Requirecomponent (typeof (Rigidbody))]public class Testrequirecomponet:monobehaviour {}


If you try to remove a dependent component, you will be prompted as follows

Rpc

Adding this property on a method makes RPC calls to the method in network traffic.

[Rpc]void Remotemethod () {}
Runtimeinitializeonloadmethodattribute

This property is only available on Unity5.
The method that added the property is automatically called when the game starts.

Class myclass{    [Runtimeinitializeonloadmethod]    static void Onruntimemethodload ()    {        Debug.Log (" Game loaded and is running ");}    }
Selectionbaseattribute

When a gameobject contains a component that uses this attribute, selecting the Gameobject,hierarchy in Sceneview automatically selects the parent of the gameobject.

Serializefield

Use this property on a variable to force the variable to be serialized. That is, you can edit the value of the variable on the editor, even if the variable is private.
The use of forced serialization of private components is often seen in UI development.
Example

public class Testserializefield:monobehaviour {    [Serializefield]    private string name;    [Serializefield]    Private Button _button;}

Sharedbetweenanimatorsattribute

For Statemachinebehaviour, different animator will share an instance of this statemachinebehaviour, which can reduce memory consumption.

Spaceattribute

Use this property to increase the number of slots on the inspector. Example:

public class Testspaceattributebylvmingbei:monobehaviour {public    int nospace1 = 0;    public int nospace2 = 0;    [Space (Ten)]    public int space = 0;    public int nospace3 = 0;}

Textareaattribute

This property can change the edit area of string on inspector to a textarea.
Example:

public class Testtextareaattributebylvmingbei:monobehaviour {    [TextArea] public    string MText;}

Tooltipattribute

This property can generate a tip for a variable, which is displayed when the mouse pointer moves over the inspector.

public class Testtooltipattributebylvmingbei:monobehaviour {    [Tooltip ("2015!")]    public int year = 0;}

Unityapicompatibilityversionattribute

Used to declare the version compatibility of the API

Unityengine.serializationformerlyserializedasattribute

This property allows the variable to be serialized with a different name, and the previous serialized value is not lost when the variable itself modifies the name.
Example:

Using unityengine;using Unityengine.serialization;public class Myclass:monobehaviour {    [Formerlyserializedas (" MyValue ")]    private string m_myvalue;    public string myvalue    {        get {return m_myvalue;}        set {M_myvalue = value;}}    }
Unityengine.editor

The package is dedicated to the editor development

Callbackorderattribute

Define the order of callback

Caneditmultipleobjects

Editor editing multiple component functions at the same time

Customeditor

Declares a class that is a custom editor

Custompreviewattribute

Marks a class as a custom preview of a specified type
New features available after Unity4.5
Example:

[Custompreview (typeof (Gameobject))]public class mypreview:objectpreview{public    override bool Haspreviewgui ()    {        return true;    }    public override void Onpreviewgui (Rect R, Guistyle background)    {        GUI. Label (R, Target.name + "is being previewed");}    }
Custompropertydrawer

tags are used when customizing Propertydrawer.
Use this property to tag when you create a propertydrawer or decoratordrawer. TODO: How to create your own attribute

Drawgizmo

You can display a custom gizmo in the scene view
The following example shows a custom gizmo in the scene view when the Gameobject with the MyScript is selected and the camera is more than 10 away.
The gizmo's picture needs to be placed in the Assets/gizmo directory.
Example:

Using unityengine;using Unityeditor;public class Myscript:monobehaviour {    }public class Myscriptgizmodrawer {        [Drawgizmo (gizmotype.selected | gizmotype.active)]    static void Drawgizmoformyscript (MyScript scr, Gizmotype gizmotype) {        Vector3 position = scr.transform.position;                if (vector3.distance (position, Camera.current.transform.position) > 10f)            gizmos.drawicon (position, " 300px-gizmo.png ");    }    }

Initializeonloadattribute

Used on class, you can run the editor script when Unity starts.
This class is required to have a static constructor.
Make an example of creating an empty gameobject.
Example:

Using unityeditor;using unityengine; [Initializeonload]class myclass{    static MyClass ()    {        editorapplication.update + = update;        Debug.Log ("Up and Running");    }        static void Update ()    {        Debug.Log ("Updating");}    }
Initializeonloadmethodattribute

Used on method, is the method version of Initializeonload.
The method must be static.

MenuItem

Used on the method, you can create a menu item in editor, click and execute the method, you can use this property to do many extension functions. Required method is static.
Example:

Using unityengine;using unityeditor;using System.collections;public class Testmenuitem:monobehaviour {    [MenuItem ("Mymenu/create gameobject")]    public static void Creategameobject () {        new Gameobject ("Lvmingbei ' s Gameobject");}    }

Preferenceitem

Use this property to customize the preference interface of unity.
Here is an example of the use of the official:

Using unityengine;using unityeditor;using System.collections;public class Ourpreferences {    //Have we loaded the pref s yet    private static bool prefsloaded = FALSE;        The Preferences public    static bool boolpreference = FALSE;        ADD Preferences section named "My Preferences" to the preferences Window    [Preferenceitem ("My Preferences")]    PU Blic static void Preferencesgui () {        //Load the Preferences        if (!prefsloaded) {            boolpreference = editorprefs. Getbool ("Boolpreferencekey", false);            prefsloaded = true;        }                Preferences GUI        boolpreference = Editorguilayout.toggle ("Bool Preference", boolpreference);                Save the Preferences        if (gui.changed)            editorprefs.setbool ("Boolpreferencekey", boolpreference);    }}

Unityeditor.callbacks

This package is a property of three callback, all of which require static methods.

Onopenassetattribute

Called after a asset is opened.
Example:

Using unityengine;using unityeditor;using unityeditor.callbacks;    public class Myassethandler {    [Onopenassetattribute (1)] public    static bool Step1 (int InstanceID, int. line) {        String name = Editorutility.instanceidtoobject (InstanceID). Name;        Debug.Log ("Open Asset step:1 (" +name+ ")");        return false; We did not handle the open    }    //Step2 have an attribute with index 2, so would be a called after Step1    [Onopena Ssetattribute (2)] public    static bool Step2 (int InstanceID, int. line) {        Debug.Log ("Open Asset step:2 (" +instance Id+ ")");        return false; We did not handle the Open    }}
Postprocessbuildattribute

The property is called after the build is completed, callback.
When you have multiple times, you can specify the order of precedence.
Example:

Using unityengine;using unityeditor;using unityeditor.callbacks;    public class Mybuildpostprocessor {    [Postprocessbuildattribute (1)] public    static void Onpostprocessbuild ( Buildtarget target, String pathtobuiltproject) {        Debug.Log (pathtobuiltproject);        }}
Postprocesssceneattribute

A function that uses this property is called before the scene is build.
The method of use is similar to Postprocessbuildattribute.

Transfer from http://blog.csdn.net/spring_shower/article/details/48708337

Unity3d Attribute Summary (Turn)

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.