Unity3D指令碼屬性

weixin_34162629發表於2014-09-15

Unity3D的指令碼屬性用法:

// JavaScript@script 
AddComponentMenu ("Transform/Follow Transform")
// CSharp
[AddComponentMenu("Transform/Follow Transform")]

以下是具體說明(部分無關緊要的不翻譯):

AddComponentMenu(函式)

允許你從其他的"Components"選單途徑新增指令碼(如果不使用AddComponentMenu那麼你只能從Components->Scripts下新增)。注意需restart起效。

ContextMenu

允許為指令碼增加一組命令,該屬性標記在函式上,而且必須是non-static函式。
假設你為一個指令碼move增加了一個ContextMenu屬性的方法Do somethine,該方法作用是Debug.Log。這個move指令碼attach在一個user身上。那麼你選中user時,在inspector皮膚中對move component右擊,就會發現一組命令,點選列印。
這個屬性一個有用之處是自動初始化。

ImageEffectOpaque 

Any Image Effect with this attribute will be rendered after opaque geometry but before transparent geometry.

This allows for effects which extensively use the depth buffer (SSAO ect) to only affect opaque pixels. This Attribute can be used to reduce the amount of visual artifacts in a scene with post processing.

ImageEffectTransformsToLDR 

When using HDR rendering it can sometime be desirable to switch to LDR rendering during ImageEffect rendering.

Using this Attribute on an image effect will cause the destination buffer to be an LDR buffer, and switch the rest of the Image Effect pipeline into LDR mode. It is the responsibility of the Image Effect that this Attribute is associated to ensure that the output is in the LDR range.

ExecuteInEditMode

預設指令碼元件僅在play mode下執行。增加這組屬性後,指令碼的callback functions在eidtor下亦可執行。注意,呼叫頻率不同於 Play Mode:
Update只發生在Scene中的一些東西變更。
OnGUI只發生在Game View收穫到一組事件。
OnRenderObject以及其他渲染事件只發生在Scene View or Game View 的 repaint 中。

HideInInspector

設定一個變數,不顯示在inspector皮膚,但是  be serialized。

NonSerialized

讓一個變數不要 serialized,這樣的話你可以保持一個公共變數,但是不顯示在inspector皮膚且不嘗試 serialize(即你改動之,下次開啟的話仍然是你申明的初始值)。

NotConvertedAttribute 

Instructs the build pipeline not to convert a type or member to the target platform。

NotRenamedAttribute 

Prevent name mangling of constructors, methods, fields and properties.

When applied to a type prevents all its members of being renamed.

It can be used to provide stable member names for access from handwritten ActionScript code or to provide .net stubs for ActionScript types when combined with NotRenamedAttribute 

RPC 

制定一個函式RPC屬性,這個方法可以通過Unity Networking來遠端訪問。該函式必須eixst on both sending and recieving party

RequireComponent 

指明自動增加對應的依賴component。例如:
[RequireComponent (typeof (Rigidbody))]
public class PlayerScript : MonoBehaviour {
    void FixedUpdate()  {
        rigidbody.AddForce(Vector3.up);
    }
}

Serializable 

申明這個屬性的class,其內部屬性可以通過inspector皮膚訪問到。例如:Vector3這樣的東東。

SerializeField 

申明一個private變數需要序列化(serialize),正常情況下你不應該需要到這個屬性。
注意,serialize是指當你在inspector皮膚改動一個值時,unity editor會自動記錄你的變更。這樣你重新開啟工程時,unity構建世界時自動使用你上次的值。

相關文章