Unity 中如何使用Attribute,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
為巴林左旗等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及巴林左旗網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計、成都做網(wǎng)站、巴林左旗網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
可以在UnityEditor的Component的Menu中增加自定義的項目。菜單可以設(shè)置多級,使用斜線/分隔即可。在Hierarchy中選中GameObject的時候,點(diǎn)擊該菜單項,就可以在GameObject上追加該Component。
例如如下代碼可以完成下圖的效果。
[AddComponentMenu("TestMenu/TestComponet")] public class TestMenu : MonoBehaviour { }
匯編級屬性,使用該屬性的Class會被認(rèn)為是EditorClass。具體用法不明。
可以在Inspector的ContextMenu中增加選項。
例如,如下代碼的效果
public class TestMenu : MonoBehaviour { [ContextMenu ("Do Something")] void DoSomething () { Debug.Log ("Perform operation"); } }
這個屬性是Unity4.5之后提供的新功能,可以在Inspector上面對變量追加一個右鍵菜單,并執(zhí)行指定的函數(shù)。
例子:
public class Sample : MonoBehaviour { [ContextMenuItem("Reset", "ResetName")] public string name = "Default"; void ResetName() { name = "Default"; } }
對一個MonoBehaviour的子類使用這個屬性,那么在同一個GameObject上面,最多只能添加一個該Class的實例。
嘗試添加多個的時候,會出現(xiàn)下面的提示。
默認(rèn)狀態(tài)下,MonoBehavior中的Start,Update,OnGUI等方法,需要在Play的狀態(tài)下才會被執(zhí)行。
這個屬性讓Class在Editor模式(非Play模式)下也能執(zhí)行。
但是與Play模式也有一些區(qū)別。
例如:
Update方法只在Scene編輯器中有物體產(chǎn)生變化時,才會被調(diào)用。
OnGUI方法只在GameView接收到事件時,才會被調(diào)用。
這個屬性可以在Inspector中變量的上面增加Header。
例子:
public class ExampleClass : MonoBehaviour { [Header("生命值")] public int CurrentHP = 0; public int MaxHP = 100; [Header("魔法值")] public int CurrentMP = 0; public int MaxMP = 0; }
在變量上使用這個屬性,可以讓public的變量在Inspector上隱藏,也就是無法在Editor中進(jìn)行編輯。
在OnRenderImage上使用,可以讓渲染順序在非透明物體之后,透明物體之前。
例子
[ImageEffectOpaque] void OnRenderImage (RenderTexture source, RenderTexture destination){ }
渲染從從HDR變?yōu)長DR 具體使用方法不明。
在string類型上使用,可以在Editor上輸入多行文字。
public class TestString : MonoBehaviour { [MultilineAttribute] public string mText; }
在變量上使用,可以指定該變量在build的時候,不要轉(zhuǎn)換為目標(biāo)平臺的類型。
在變量上使用,在Flash平臺build的時候,對該變量不進(jìn)行類型檢查。
Unity5.0中已經(jīng)移除了這個屬性。
禁止對變量和方法進(jìn)行重命名。
Unity5.0中已經(jīng)移除了這個屬性。
在int或者float類型上使用,限制輸入值的范圍
public class TestRange : MonoBehaviour { [Range(0, 100)] public int HP; }
在Class上使用,添加對另一個Component的依賴。
當(dāng)該Class被添加到一個GameObject上的時候,如果這個GameObject不含有依賴的Component,會自動添加該Component。
且該Componet不可被移除。
例子
[RequireComponent(typeof(Rigidbody))] public class TestRequireComponet : MonoBehaviour { }
如果嘗試移除被依賴的Component,會有如下提示
在方法上添加該屬性,可以網(wǎng)絡(luò)通信中對該方法進(jìn)行RPC調(diào)用。
[RPC] void RemoteMethod(){ }
此屬性僅在Unity5上可用。
在游戲啟動時,會自動調(diào)用添加了該屬性的方法。
class MyClass { [RuntimeInitializeOnLoadMethod] static void OnRuntimeMethodLoad () { Debug.Log("Game loaded and is running"); } }
當(dāng)一個GameObject含有使用了該屬性的Component的時候,在SceneView中選擇該GameObject,Hierarchy上面會自動選中該GameObject的Parent。
在變量上使用該屬性,可以強(qiáng)制該變量進(jìn)行序列化。即可以在Editor上對變量的值進(jìn)行編輯,即使變量是private的也可以。
在UI開發(fā)中經(jīng)??梢姷綄rivate的組件進(jìn)行強(qiáng)制序列化的用法。
例子
public class TestSerializeField : MonoBehaviour { [SerializeField] private string name; [SerializeField] private Button _button; }
用于StateMachineBehaviour上,不同的Animator將共享這一個StateMachineBehaviour的實例,可以減少內(nèi)存占用。
使用該屬性可以在Inspector上增加一些空位。 例子:
public class TestSpaceAttributeByLvmingbei : MonoBehaviour { public int nospace1 = 0; public int nospace2 = 0; [Space(10)] public int space = 0; public int nospace3 = 0; }
該屬性可以把string在Inspector上的編輯區(qū)變成一個TextArea。
例子:
public class TestTextAreaAttributeByLvmingbei : MonoBehaviour { [TextArea] public string mText; }
這個屬性可以為變量上生成一條tip,當(dāng)鼠標(biāo)指針移動到Inspector上時候顯示。
public class TestTooltipAttributeByLvmingbei : MonoBehaviour { [Tooltip("This year is 2015!")] public int year = 0; }
用來聲明API的版本兼容性
該屬性可以令變量以另外的名稱進(jìn)行序列化,并且在變量自身修改名稱的時候,不會丟失之前的序列化的值。
例子:
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; } } }
該package為Editor開發(fā)專用
定義Callback的順序
Editor同時編輯多個Component的功能
聲明一個Class為自定義Editor的Class
將一個class標(biāo)記為指定類型的自定義預(yù)覽
Unity4.5以后提供的新功能
例子:
[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"); } }
標(biāo)記自定義PropertyDrawer時候使用。
當(dāng)自己創(chuàng)建一個PropertyDrawer或者DecoratorDrawer的時候,使用該屬性來標(biāo)記。 TODO: 如何創(chuàng)建屬于自己的Attribute
可以在Scene視圖中顯示自定義的Gizmo
下面的例子,是在Scene視圖中,當(dāng)掛有MyScript的GameObject被選中,且距離相機(jī)距離超過10的時候,便顯示自定義的Gizmo。
Gizmo的圖片需要放入Assets/Gizmo目錄中。
例子:
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"); } }
在Class上使用,可以在Unity啟動的時候,運(yùn)行Editor腳本。
需要該Class擁有靜態(tài)的構(gòu)造函數(shù)。
做一個創(chuàng)建一個空的gameobject的例子。
例子:
using UnityEditor; using UnityEngine; [InitializeOnLoad] class MyClass { static MyClass () { EditorApplication.update += Update; Debug.Log("Up and running"); } static void Update () { Debug.Log("Updating"); } }
在Method上使用,是InitializeOnLoad的Method版本。
Method必須是static的。
在方法上使用,可以在Editor中創(chuàng)建一個菜單項,點(diǎn)擊后執(zhí)行該方法,可以利用該屬性做很多擴(kuò)展功能。 需要方法為static。
例子:
using UnityEngine; using UnityEditor; using System.Collections; public class TestMenuItem : MonoBehaviour { [MenuItem ("MyMenu/Create GameObject")] public static void CreateGameObject() { new GameObject("lvmingbei's GameObject"); } }
使用該屬性可以定制Unity的Preference界面。
在這里就使用官方的例子:
using UnityEngine; using UnityEditor; using System.Collections; public class OurPreferences { // Have we loaded the prefs 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")] public 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); } }
這個package中是三個Callback的屬性,都需要方法為static的。
在打開一個Asset后被調(diào)用。
例子:
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 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 } }
該屬性是在build完成后,被調(diào)用的callback。
同時具有多個的時候,可以指定先后順序。
例子:
using UnityEngine; using UnityEditor; using UnityEditor.Callbacks; public class MyBuildPostprocessor { [PostProcessBuildAttribute(1)] public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) { Debug.Log( pathToBuiltProject ); } }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
本文題目:Unity中如何使用Attribute
分享路徑:http://aaarwkj.com/article38/pdppsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、外貿(mào)網(wǎng)站建設(shè)、定制網(wǎng)站、自適應(yīng)網(wǎng)站、外貿(mào)建站、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)