前言:
在使用Unity3D的過程中研侣,會經(jīng)常用到許多的特性Attribute咆课,我基本上把官方文檔里列出的都記錄下來,不定期更新
一、HelpURLAttribute
從字面意思理解,是查看幫助時,跳轉(zhuǎn)到指定的頁面。
如下圖:
對應(yīng)著藍(lán)色小書的圖標(biāo)抹估,點擊以后會跳轉(zhuǎn)到配置的URL瓷式。
二踱卵、RangeAttribute:限定int或float的取值范圍。
Attribute used to make a float or int variable in a script be restricted to a specific range.
When this attribute is used, the float or int will be shown as a slider in the Inspector instead of the default number field.
當(dāng)在int或float上應(yīng)用RangeAttribute特性時,在Inspector面板中积蜻,顯示的將是一個slider滑動條丙笋,而不是默認(rèn)的數(shù)值字段怠肋。
[Range(0.1f,0.9f)]
float ratio;
三、RequireComponentAttribute
自動添加所要依賴的組件础芍,如將一個Script做為一個GameObject的組件酪惭,而這個Script需要訪問Rigidbody組件,
通過應(yīng)用該屬性者甲,可以自動的添加Rigidbody組件到當(dāng)前的GameObject中春感,避免設(shè)置錯誤。
*前提是:如果當(dāng)前的Script已經(jīng)添加到了GameObject虏缸,這時候你再應(yīng)用RequireComponent特性是無效的鲫懒,
你必須刪除 再重新添加,才會檢測刽辙。
using UnityEngine;
// PlayerScript requires the GameObject to have a Rigidbody component
[RequireComponent(typeof(Rigidbody))]
public class PlayerScript : MonoBehaviour
{
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.AddForce(Vector3.up);
}
}
四窥岩、TooltipAttribute
在Inspector面板中,為一個字段Field指定一個提示宰缤。
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
[Tooltip("Health value between 0 and 100.")]
public int health = 0;
}
五颂翼、HideInInspectorAttribute
讓一個可被序列化的字段,不要顯示在Inspector面板中慨灭,防止修改朦乏。
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
[HideInInspector]
public int p = 5;
}
六、ExecuteInEditMode
讓MonoBehaviour腳本的所有實例氧骤,在編輯模式下可運行呻疹。
這些函數(shù)的調(diào)用并不會像在PlayMode下那樣:
The functions are not called constantly like they are in play mode.
- Update is only called when something in the scene changed.
- OnGUI is called when the Game View recieves an Event.
- OnRenderObject and the other rendering callback functions are called on every repaint of the Scene View or Game View.
只有窗口在發(fā)生改變 ,接觸新的事件筹陵,重繪后才會調(diào)用刽锤。
*默認(rèn)MonoBehaviour的腳本只能在運行模式下執(zhí)行镊尺。
using UnityEngine;
[ExecuteInEditMode]
public class PrintAwake : MonoBehaviour
{
void Awake()
{
Debug.Log("Editor causes this Awake");
}
void Update()
{
Debug.Log("Editor causes this Update");
}
}
七、DisallowMultipleComponent
禁止一個組件被重復(fù)的添加多次并思。
*如果當(dāng)前GameObject已經(jīng)存在了多個相同的組件庐氮,是不會有影響的,但應(yīng)用了特性以后宋彼,就無法再次添加弄砍。
[DisallowMultipleComponent]
public class testEdit : MonoBehaviour {...}
八、DelayedAttribute
在運行時宙暇,我們修改Inspector面板中的字段,會即時返回新的值议泵,應(yīng)用Delayed特性占贫,只有在用戶按下回車Enter或是
焦點離開時才會返回新值,通過用于調(diào)試階段先口。
*只能應(yīng)用于字段型奥,不可在類或其它目標(biāo)元素上使用。
九碉京、SpaceAttribute
間隔距離厢汹,在Inspector中,可以設(shè)置元素與元素之間的間隔谐宙。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testEdit : MonoBehaviour {
public int a = 10;
[Space(50)]
public int b = 11;
}
十烫葬、TextAreaAttribute
可以在一個高自由度并且可滾動的文本區(qū)域里編輯string字符串,如果字符串比較長凡蜻,比較適用搭综。
參數(shù):
minLines:文本區(qū)域最小行數(shù)
maxLines:文本區(qū)域最大行數(shù),超過最大行數(shù)划栓,會出現(xiàn)滾動條兑巾。
[TextArea(1,5)]
public string abc;
十一、MultilineAttribute
在一個支持多行的文本區(qū)域內(nèi)編輯string字符串忠荞,他和TextAreaAttribute不同蒋歌,MultilineAttribute的TextArea沒有滾動條。
[Multiline(1,5)]
public string abc;
十二委煤、ContextMenu
向Inspector面板中腳本Script的上下文菜單(快捷堂油,右鍵),添加一條指令碧绞,當(dāng)選擇該命令時称诗,函數(shù)會執(zhí)行。
*只能用于非靜態(tài)函數(shù)
public class ContextTesting : MonoBehaviour {
/// Add a context menu named "Do Something" in the inspector
/// of the attached script.
[ContextMenu ("Do Something")]
void DoSomething () {
Debug.Log ("Perform operation");
}
}
預(yù)定義的一些方法头遭,如Reset寓免,是可以進(jìn)行重載的癣诱。
十三、ContextMenuItemAttribute
在Inspector面板中袜香,為字段field添加一個快捷的菜單撕予。
[Multiline][ContextMenuItem("Reset", "ResetString")]
public string abc;
public void ResetString()
{
abc = "";
}
十四、CreateAssetMenuAttribute
快速的創(chuàng)建ScriptableObject派生類的實例蜈首,并存儲成以“.asset"結(jié)尾的文件实抡,ScriptableObject的派生類可以存儲為外部的文件,圖形化編輯對象數(shù)據(jù)欢策,一些靜態(tài)的數(shù)據(jù)吆寨,動態(tài)的加載,ScriptableObject是一種解決方案踩寇,具體見另一篇文章的說明:
http://www.reibang.com/p/da578e55ca47
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "xxxx",menuName = "xxx/xxx")]
public class testEdit : ScriptableObject {
public int a = 10;
public int b = 11;
public int c = 12;
[Multiline][ContextMenuItem("Reset", "ResetString")]
public string abc;
}
說明:
fileName:生成asset文件的文件名啄清。
menuName:在Assets/Create上子菜單的名字。
十五俺孙、ColorUsageAttribute
顏色選擇器辣卒,color picker,只能應(yīng)用在Color字段上睛榄。
默認(rèn)參數(shù)為是否顯示alpha荣茫,具體使用看下官方文檔的參數(shù)描述,這里不加代碼了
十六场靴、AddComponentMenu
把添加Script的操作放在Component菜單下啡莉,來替代Component/Scripts,因為里面的腳本可能非常多旨剥,基本上沒有實用價值
AddComponentMenu的方便之處在于如果你當(dāng)前場景內(nèi)票罐,有多個GameObjects需要添加同一個腳本,那么使用同時選中
這些地象泞边,并打開Component菜單選中要添加的腳本就可以一次性添加了该押。
[AddComponentMenu("Example/testEdits")]
public class testEdit : MonoBehaviour {
十七、AssemblyIsEditorAssembly
添加該特性的任意類阵谚,都會被視為Editor編輯器類蚕礼。只有用于Editor模式下。
十八梢什、PreferBinarySerialization
只能用在ScriptableObject的派生類上(使用在其它類型上面會被忽略)奠蹬,修改序列化的模式為二進(jìn)制,而不是YAML嗡午, 當(dāng)你的資源比較大的時候囤躁,保存成二進(jìn)制,生成的數(shù)據(jù)會更加的緊湊,從而提高程序的讀寫性能狸演。
[CreateAssetMenu]
[PreferBinarySerialization]
public class testEdit : ScriptableObject {
public Color a;
public int b = 11;
public int c = 12;
[Multiline][ContextMenuItem("Reset", "ResetString")]
public string abc;
}
用記事本打開生成后的asset言蛇,會發(fā)現(xiàn)都是二進(jìn)制的數(shù)據(jù)。
十九宵距、RuntimeInitializeOnLoadMethodAttribute
在運行時腊尚,當(dāng)前類初始化完成,自動調(diào)用被該特性應(yīng)用的靜態(tài)函數(shù),這和static靜態(tài)構(gòu)造函數(shù)還不一樣满哪,static靜態(tài)構(gòu)造函數(shù)是在所有的方法之前運行的婿斥,而RuntimeInitializeOnLoadMethod特性的方法是Awake方法之后執(zhí)行的(如果是MonoBehaviour派生類)。
如果一個類中有多個靜態(tài)方法使用了RuntimeInitializeOnLoadMethod特性哨鸭,執(zhí)行順序是不固定的民宿。
[RuntimeInitializeOnLoadMethod]
static void OnRuntimeMethodLoad()
{
Debug.Log("After scene is loaded and game is running");
}
[RuntimeInitializeOnLoadMethod]
static void OnSecondRuntimeMethodLoad()
{
Debug.Log("SecondMethod After scene is loaded and game is running.");
}
二十、SelectionBaseAttribute
設(shè)置基礎(chǔ)選中對象像鸡,應(yīng)用該標(biāo)識一個對象為選中對象活鹰,當(dāng)我們在scene view中選擇一個objects的時候,u3d會返回給我們是適合的對象坟桅,比如你選中的對象是prefab的一部分华望,默認(rèn)會返回節(jié)點的根對象蕊蝗,默認(rèn)根對象被設(shè)置成了基礎(chǔ)選中對象仅乓,你可以修改他,讓其它的對象成為基礎(chǔ)選中對象蓬戚,比如根對象可能就是一個空的GameObject夸楣,而我們要實際查看編輯的對象是子節(jié)點,這樣我們可以將子節(jié)點中添加的腳本應(yīng)用SelectionBase特性子漩。
我將腳本加到Camera_Offset后豫喧,成為了默認(rèn)的選中對象,這樣每次我在場景中選中時幢泼,Camera_Offset會被選擇紧显,并高亮顯示。
二十一缕棵、SerializeField
強(qiáng)制去序列化一個私有的字段field.默認(rèn)情況下孵班,當(dāng)u3d在序列化腳本的時候,只會序列化public的字段招驴,這是u3d內(nèi)部的實現(xiàn)的序列化篙程,并不是 .NET's serialization的實現(xiàn)。
另一點别厘,私有字段虱饿,你不希望派生類訪問,但你希望在Inspector中可以進(jìn)行配置,也可以應(yīng)用SerializeField來解決氮发。
using UnityEngine;
public class SomePerson : MonoBehaviour
{
//This field gets serialized because it is public.
public string name = "John";
//This field does not get serialized because it is private.
private int age = 40;
//This field gets serialized even though it is private
//because it has the SerializeField attribute applied.
[SerializeField]
private bool hasHealthPotion = true;
void Update()
{
}
}
二十二渴肉、SharedBetweenAnimatorsAttribute
狀態(tài)機(jī)組件,沒有在項目中使用過折柠,mark下宾娜,在簡書里新建一篇叫StateMachineBehaviour的文章,實際測試案例后扇售,回來補(bǔ)充前塔。
二十三、HeaderAttribute
在Inspector面板中承冰,為field字段添加頭信息华弓,增強(qiáng)描述。
The header is done using a DecoratorDrawer.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
[Header("Health Settings")]
public int health = 0;
public int maxHealth = 100;
[Header("Shield Settings")]
public int shield = 0;
public int maxShield = 0;
}
二十四困乒、 GUITargetAttribute
設(shè)置OnGUI方法在哪一個Display下顯示寂屏,默認(rèn)是所有的Display均顯示.
[GUITarget(0,1,new int[]{2,3,4})]
void OnGUI()
{
if (GUI.Button (new Rect (0, 0, 128, 128), "Test")) {
Debug.Log ("blahblahblah....");
}
}
說明:
提供了如下參數(shù):
displayIndex Display index.display 索引
displayIndex1 Display index. display索引
displayIndexList Display index list.display索引列表
二十五、未完待續(xù)
到此為止,如果大家發(fā)現(xiàn)有什么不對的地方,歡迎指正,共同提高,感謝您的閱讀!
編輯于2018.7.16
現(xiàn)在是凌晨2:08娜搂,剛結(jié)束和喜歡的人聊天迁霎,我此刻的感受并不好,她的一句my friend,感覺距離拉到了銀河系百宇,這種憤怒輻射到讓我討厭我生活的很多方面考廉,我希望這種憤怒能最終給我?guī)碚娴牧α浚?/p>