国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Unity教程之-Unity Attribute的使用總結(jié)

2019-11-06 10:03:28
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

舉兩個(gè)例子,在變量上使用[SerializeFiled]屬性,可以強(qiáng)制讓變量進(jìn)行序列化,可以在Unity的Editor上進(jìn)行賦值。在Class上使用[RequireComponent]屬性,就會(huì)在Class的GameObject上自動(dòng)追加所需的Component。

以下是Unity官網(wǎng)文檔中找到的所有Attribute,下面將按照順序,逐個(gè)對(duì)這些Attribute進(jìn)行說(shuō)明和小的測(cè)試。部分例子使用了Unity官方的示例。

UnityEngine

AddComponentMenu

可以在UnityEditor的Component的Menu中增加自定義的項(xiàng)目。菜單可以設(shè)置多級(jí),使用斜線/分隔即可。在Hierarchy中選中GameObject的時(shí)候,點(diǎn)擊該菜單項(xiàng),就可以在GameObject上追加該Component。例如如下代碼可以完成下圖的效果。

123[AddComponentMenu("TestMenu/TestComponet")]public class TestMenu : MonoBehaviour {}

f:id:lvmingbei:20150406160818p:plain

AssemblyIsEditorAssembly

匯編級(jí)屬性,使用該屬性的Class會(huì)被認(rèn)為是EditorClass。具體用法不明。

ContextMenu

可以在Inspector的ContextMenu中增加選項(xiàng)。例如,如下代碼的效果

123456publicclassTestMenu:MonoBehaviour{    [ContextMenu("Do Something")]    voidDoSomething(){        Debug.Log("Perform Operation");    }}

f:id:lvmingbei:20150406162538p:plain

ContextMenuItemAttribute

這個(gè)屬性是Unity4.5之后提供的新功能,可以在Inspector上面對(duì)變量追加一個(gè)右鍵菜單,并執(zhí)行指定的函數(shù)。例子:

1234567public class Sample : MonoBehaviour {    [ContextMenuItem("Reset", "ResetName")]    public string name = "Default";    void ResetName() {        name = "Default";    }}

f:id:lvmingbei:20150406164234g:plain

DisallowMultipleComponent

對(duì)一個(gè)MonoBehaviour的子類(lèi)使用這個(gè)屬性,那么在同一個(gè)GameObject上面,最多只能添加一個(gè)該Class的實(shí)例。嘗試添加多個(gè)的時(shí)候,會(huì)出現(xiàn)下面的提示。f:id:lvmingbei:20150406165035p:plain

ExecuteInEditMode

默認(rèn)狀態(tài)下,MonoBehavior中的Start,Update,OnGUI等方法,需要在Play的狀態(tài)下才會(huì)被執(zhí)行。這個(gè)屬性讓Class在Editor模式(非Play模式)下也能執(zhí)行。但是與Play模式也有一些區(qū)別。例如:Update方法只在Scene編輯器中有物體產(chǎn)生變化時(shí),才會(huì)被調(diào)用。OnGUI方法只在GameView接收到事件時(shí),才會(huì)被調(diào)用。

HeaderAttribute

這個(gè)屬性可以在Inspector中變量的上面增加Header。例子:

123456789publicclassExampleClass:MonoBehaviour{    [Header("生命值")]    publicintCurrentHP=0;    publicintMaxHP=100;     [Header("魔法值")]    publicintCurrentMP=0;    publicintMaxMP=0;}

f:id:lvmingbei:20150406171544p:plain

HideInInspector

在變量上使用這個(gè)屬性,可以讓public的變量在Inspector上隱藏,也就是無(wú)法在Editor中進(jìn)行編輯。

ImageEffectOpaque

在OnRenderImage上使用,可以讓渲染順序在非透明物體之后,透明物體之前。例子

123[ImageEffectOpaque]void OnRenderImage (RenderTexture source, RenderTexture destination){}
ImageEffectTransformsToLDR

渲染從從HDR變?yōu)長(zhǎng)DR 具體使用方法不明。

MultilineAttribute

在string類(lèi)型上使用,可以在Editor上輸入多行文字。

1234publicclassTestString:MonoBehaviour{    [MultilineAttribute]    publicstringmText;}

f:id:lvmingbei:20150406183813p:plain

NotConvertedAttribute

在變量上使用,可以指定該變量在build的時(shí)候,不要轉(zhuǎn)換為目標(biāo)平臺(tái)的類(lèi)型。

NotFlashValidatedAttribute

在變量上使用,在Flash平臺(tái)build的時(shí)候,對(duì)該變量不進(jìn)行類(lèi)型檢查。Unity5.0中已經(jīng)移除了這個(gè)屬性。

NotRenamedAttribute

禁止對(duì)變量和方法進(jìn)行重命名。Unity5.0中已經(jīng)移除了這個(gè)屬性。

PRopertyAttribute
RangeAttribute

在int或者float類(lèi)型上使用,限制輸入值的范圍

1234public class TestRange : MonoBehaviour{    [Range(0, 100)] public int HP;}
RequireComponent

在Class上使用,添加對(duì)另一個(gè)Component的依賴。當(dāng)該Class被添加到一個(gè)GameObject上的時(shí)候,如果這個(gè)GameObject不含有依賴的Component,會(huì)自動(dòng)添加該Component。且該Componet不可被移除。

例子

1234[RequireComponent(typeof(Rigidbody))]publicclassTestRequireComponet:MonoBehaviour{ }

f:id:lvmingbei:20150406191049g:plain如果嘗試移除被依賴的Component,會(huì)有如下提示f:id:lvmingbei:20150406191137p:plain

RPC

在方法上添加該屬性,可以網(wǎng)絡(luò)通信中對(duì)該方法進(jìn)行RPC調(diào)用。

123[RPC]void RemoteMethod(){}
RuntimeInitializeOnLoadMethodAttribute

此屬性僅在Unity5上可用。在游戲啟動(dòng)時(shí),會(huì)自動(dòng)調(diào)用添加了該屬性的方法。

12345678classMyClass{    [RuntimeInitializeOnLoadMethod]    staticvoidOnRuntimeMethodLoad()    {        Debug.Log("Game loaded and is running");    }}
SelectionBaseAttribute

當(dāng)一個(gè)GameObject含有使用了該屬性的Component的時(shí)候,在SceneView中選擇該GameObject,Hierarchy上面會(huì)自動(dòng)選中該GameObject的Parent。

SerializeField

在變量上使用該屬性,可以強(qiáng)制該變量進(jìn)行序列化。即可以在Editor上對(duì)變量的值進(jìn)行編輯,即使變量是private的也可以。在UI開(kāi)發(fā)中經(jīng)常可見(jiàn)到對(duì)private的組件進(jìn)行強(qiáng)制序列化的用法。例子

1234567public class TestSerializeField : MonoBehaviour {    [SerializeField]    private string name;     [SerializeField]    private Button _button;}

f:id:lvmingbei:20150406194030p:plain

SharedBetweenAnimatorsAttribute

用于StateMachineBehaviour上,不同的Animator將共享這一個(gè)StateMachineBehaviour的實(shí)例,可以減少內(nèi)存占用。

SpaceAttribute

使用該屬性可以在Inspector上增加一些空位。 例子:

1234567publicclassTestSpaceAttributeByLvmingbei:MonoBehaviour{    publicintnospace1=0;    publicintnospace2=0;    [Space(10)]    publicintspace=0;    publicintnospace3=0;}

f:id:lvmingbei:20150407121446p:plain

TextAreaAttribute

該屬性可以把string在Inspector上的編輯區(qū)變成一個(gè)TextArea。例子:

1234public class TestTextAreaAttributeByLvmingbei : MonoBehaviour {    [TextArea]    public string mText;}

f:id:lvmingbei:20150407121811p:plain

TooltipAttribute

這個(gè)屬性可以為變量上生成一條tip,當(dāng)鼠標(biāo)指針移動(dòng)到Inspector上時(shí)候顯示。

1234publicclassTestTooltipAttributeByLvmingbei:MonoBehaviour{    [Tooltip("This year is 2015!")]    publicintyear=0;}

f:id:lvmingbei:20150407122412p:plain

UnityAPICompatibilityVersionAttribute

用來(lái)聲明API的版本兼容性

UnityEngine.Serialization

FormerlySerializedAsAttribute

該屬性可以令變量以另外的名稱進(jìn)行序列化,并且在變量自身修改名稱的時(shí)候,不會(huì)丟失之前的序列化的值。例子:

1234567891011using 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

該package為Editor開(kāi)發(fā)專用

CallbackOrderAttribute

定義Callback的順序

CanEditMultipleObjects

Editor同時(shí)編輯多個(gè)Component的功能

CustomEditor

聲明一個(gè)Class為自定義Editor的Class

CustomPreviewAttribute

將一個(gè)class標(biāo)記為指定類(lèi)型的自定義預(yù)覽Unity4.5以后提供的新功能例子:

12345678910111213[CustomPreview(typeof(GameObject))]publicclassMyPreview:ObjectPreview{    publicoverrideboolHaspreviewGUI()    {        returntrue;    }     publicoverridevoidOnPreviewGUI(Rectr,GUIStylebackground)    {        GUI.Label(r,target.name+" is being previewed");    }}
CustomPropertyDrawer

標(biāo)記自定義PropertyDrawer時(shí)候使用。當(dāng)自己創(chuàng)建一個(gè)PropertyDrawer或者DecoratorDrawer的時(shí)候,使用該屬性來(lái)標(biāo)記。 TODO: 如何創(chuàng)建屬于自己的Attribute

DrawGizmo

可以在Scene視圖中顯示自定義的Gizmo下面的例子,是在Scene視圖中,當(dāng)掛有MyScript的GameObject被選中,且距離相機(jī)距離超過(guò)10的時(shí)候,便顯示自定義的Gizmo。Gizmo的圖片需要放入Assets/Gizmo目錄中。例子:

123456789101112131415161718using 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");    }    }

f:id:lvmingbei:20150410162651p:plain

InitializeOnLoadAttribute

在Class上使用,可以在Unity啟動(dòng)的時(shí)候,運(yùn)行Editor腳本。需要該Class擁有靜態(tài)的構(gòu)造函數(shù)。做一個(gè)創(chuàng)建一個(gè)空的gameobject的例子。例子:

1234567891011121314151617usingUnityEditor;usingUnityEngine; [InitializeOnLoad]classMyClass{    staticMyClass()    {        Editorapplication.update+=Update;        Debug.Log("Up and running");    }        staticvoidUpdate()    {        Debug.Log("Updating");    }}
InitializeOnLoadMethodAttribute

在Method上使用,是InitializeOnLoad的Method版本。Method必須是static的。

MenuItem

在方法上使用,可以在Editor中創(chuàng)建一個(gè)菜單項(xiàng),點(diǎn)擊后執(zhí)行該方法,可以利用該屬性做很多擴(kuò)展功能。 需要方法為static。例子:

1234567891011using UnityEngine;using UnityEditor;using System.Collections; public class TestMenuItem : MonoBehaviour {     [MenuItem ("MyMenu/Create GameObject")]    public static void CreateGameObject() {        new GameObject("lvmingbei's GameObject");    }}

f:id:lvmingbei:20150412204353g:plain

PreferenceItem

使用該屬性可以定制Unity的Preference界面。在這里就使用官方的例子:

12345678910111213141516171819202122232425262728usingUnityEngine;usingUnityEditor;usingSystem.Collections; publicclassOurPreferences{    // Have we loaded the prefs yet    privatestaticboolprefsLoaded=false;        // The Preferences    publicstaticboolboolPreference=false;        // Add preferences section named "My Preferences" to the Preferences Window    [PreferenceItem("My Preferences")]    publicstaticvoidPreferencesGUI(){        // 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);    }}

f:id:lvmingbei:20150412210832p:plain

UnityEditor.Callbacks

這個(gè)package中是三個(gè)Callback的屬性,都需要方法為static的。

OnOpenAssetAttribute

在打開(kāi)一個(gè)Asset后被調(diào)用。例子:

1234567891011121314151617181920using 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 has an attribute with index 2, so will be called after step1    [OnOpenAssetAttribute(2)]    public static bool step2(int instanceID, int line) {        Debug.Log("Open Asset step: 2 ("+instanceID+")");        return false; // we did not handle the open    }}
PostProcessBuildAttribute

該屬性是在build完成后,被調(diào)用的callback。同時(shí)具有多個(gè)的時(shí)候,可以指定先后順序。例子:

12345678910usingUnityEngine;usingUnityEditor;usingUnityEditor.Callbacks;    publicclassMyBuildPostprocessor{    [PostProcessBuildAttribute(1)]    publicstaticvoidOnPostprocessBuild(BuildTargettarget,stringpathToBuiltProject){        Debug.Log(pathToBuiltProject);        }}
PostProcessSceneAttribute

使用該屬性的函數(shù),在scene被build之前,會(huì)被調(diào)用。具體使用方法和PostProcessBuildAttribute類(lèi)似。

好了本篇unity3d教程到此結(jié)束,下篇我 們?cè)贂?huì)!


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宿松县| 肇庆市| 湄潭县| 团风县| 蒙阴县| 阳新县| 白朗县| 镇康县| 临城县| 阳信县| 香格里拉县| 张家界市| 从化市| 东阳市| 云和县| 罗源县| 唐海县| 姚安县| 昆山市| 阿拉尔市| 汶川县| 建德市| 剑川县| 读书| 虎林市| 临清市| 商水县| 大竹县| 双辽市| 垦利县| 湖口县| 资溪县| 奉新县| 和平区| 甘谷县| 盐城市| 屏山县| 姚安县| 通渭县| 乡宁县| 古田县|