There are still many attributes in Unity. unityattribute

Source: Internet
Author: User

There are still many attributes in Unity. unityattribute
Reprinted please indicate the source:

More comprehensive content please see my game pretty cool address:


Use the C # language of Unity andProperty (Attribute)Class Definition and variable definition or differentiate other variables, you can set a special behavior. * 1

For example, you add the [SerializeField] attribute variable and the private variable identifier for serialization.


Int count;

Serializing your 'value is to store the scene and premade bodies. Because the value is stored in meta metadata and set in Inspector checker.


You can also force the specified component to add the object to the component. You will add [RequireComponent] * 2*3.

[RequireComponent (typeof (Rigidbody)]

Public classAttributeSample: MonoBehaviour {


You can set behaviors other than the Special variable naming conventions when using attributes. Unity uses attributes to set the actions of special edits and methods.


Rule attributes

Attributes are placed before the form variables and class definitions of [below. If you want to set multiple attributes, separate them with commas.

For example, if you want to set the SerializeField and range, such as the description attribute.

[SerializeField, Range (0, 5)]

Int count;

This attribute applies to all variables that are immediately defined. For example, if multiple variables are defined in a declaration

[SerializeField, Range (0, 5)]

Int count3, count4;

And suitable for arrays, is applicable to all variables, so that it contains.

[SerializeField, Range (0, 5)]

Int [] counts;

Unified Standard attributes

Attributes of Inspector to be extended

Expanded Inspector checker behavior selection field.

• SerializeField

Serialization of private or protected values.

It is useful if you want to edit the scene view.


Int count;


• TooltipAttribute

If the mouse cursor is on a field, the description text is displayed.

[SerializeField, TooltipAttribute ("plaintext")]

Int count5;


• SpaceAttribute

Set the space between fields.

[SerializeField, Space (15)]



• HeaderAttribute

Set the title, that is, the header of the field. Note that the title gives all projects and adds HeaderAttribute to the list.

[SerializeField, HeaderAttribute ("Title")]

Int count7;


• MultilineAttribute

Set text fields for multi-line input.

[SerializeField, MultilineAttribute (2)]

String message1;


• TextAreaAttribute

Set text fields for multi-line input. You can set the maximum and minimum number of rows.

[SerializeField, TextAreaAttribute (2, 5)]

String message2;


• HideInInspector

This public serialization field is hidden from the Inspector checker. If the parameters are hidden, the extended editor has built-in parameters and reference relationships.

Please note that it cannot be edited from the checker. HideInInspector is used to hide a serialized value in building a unity scenario. Initialization will also be ignored when a field variable is declared.

Use NonSerializable serialization and HideInInspector.


Public int count8;

• NonSerializable

Serialization does not prevent you from disappearing from the checker.

[System. NonSerialized]

Public int count9;


• FormerlySerializedAs

When you change the variable name, keep the information modified.

In the field names of unity persistence information, the field names are changed and destroyed. When you can specify FormerlySerializedAs, this information will be taken to the name of the target variable.

Public float first;

First, all variables first. I tried to change the second variable. And add the FormerlySerializedAs attribute, formerly "first ".

[FormerlySerializedAs ("first")]

Public float second;

Even if you change the value of the variable name first after the variable name second, it is retained. If you want to delete FormerlySerializedAs and various troubles when using the migration in other scenarios, please note.


Attributes associated with the behavior of components

• RequireComponentAttribute

Add the behavior of the component to the object. For example, a rigid body ......

[RequireComponent (typeof (Rigidbody)]

Public classAttributeSample: MonoBehaviour {



• DisallowMultipleComponent

Several components cannot be added to the same object (that is, they cannot be repeated ).


Public classAttributeSample: MonoBehaviour {



• ContextMenuAttribute

I want to call a method from the context menu (right-click) of the component. For example, it is useful if you do not set passively at runtime.

[ContextMenu ("Init")]

Void Init (){



Attributes that affect game behaviors


It is used when RPC is executed. It is used for photon and communication in NetworkView. This attribute is a cross-network call method.


Void Damage (){



• ImageEffectTransformsToLDR

It seems that HDR and LDR are converted. How to Use unknown.

• ImageEffectOpaque

Make OnRenderImage transparent.


Void OnRenderImage (RenderTexture source, RenderTexture destination ){



• MonoPInvokeCallbackAttribute

The registration method in C # (managed code) can be called from c ++ (unmanaged code.


• DLLImport

The C ++ (unmanaged code) method can be called from c.

[DllImport ("DLLName")]

Private static extern void MethodName ();


Attribute associated with the action of the Editor

Is the attribute that can affect the scenario from the top of the editor.

• AddComponentMenu

Set the path. When the AddComponent button and menu bar component are specified. If you do not use your Script/Namespace or component name.

[AddComponentMenu ("Sample/TestCode")]

Public class AttributeSample: MonoBehaviour {



• ExecuteInEditMode

Status. Used to verify whether the job is running.


Public classAttributeSample: MonoBehaviour {}

The ExecuteInEditMode operation hotspot image code can be serialized in the form of value serialization → assembly and injection, without damaging the value and possibility. Because of this, attention is needed, because the scenario that needs to be used depends on static code or initialization and so on ......

For example, it is the first three, the code changes and the code changes for running and compiling the following code, and print 0.

Static int sCount = 0;

Void Start (){

SCount = 3;


Void Update (){

Debug. Log ("export:" + sCount );


• SelectionBaseAttribute

Select the component to use this attribute in the scenario.


Public class AttributeSample: MonoBehaviour {}

For example, if the SelectionBaseAttribute granted to the Renderer model of the parent object is selected in the scenario view model, select the parent object selected by the scenario view component.

Is the SelectionBase object of the game object, if it is not a batch based on the behavior. Grant gets the object of a game object that is not granted a sphere.


Related Article

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: 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.