針對Class部分使用的 Attribue
AddComponentMenu
表示在Unity中,要從哪個路徑添加此Component
[AddComponentMenu("MyScripts/My C# Script")] 表示可從清單Component -> MyScripts -> My C# Script 找到並添加
[AddComponentMenu("")] 表示不提供UI給使用者添家
不寫的話是預設於 Component -> Scripts -> [namespace] -> [Script FileName]
RequireComponent
需求Component
[RequireComponent(typeof(Rigidbody2D))] 添加此Component同時,會幫我添加Rigidbody2D
CreateAssetMenu
為UnityEditor的Asset ->Create 添加項目,此項目可建立使用此Attribue的ScriptableObject class
[CreateAssetMenu(fileName = "Item" , menuName = "TestAsset/Temp", order = 1))]
建立名子為Item的Asset,項目位於 Asset -> Create -> TestAsset -> Temp,顯示順序為1
ExecuteInEditMode
原本只在開始遊戲時才會呼叫的Start , Awake... 類,變成在編輯模式時就會執行
CustomEditor
強化特定Component的編輯介面 (Inspector)
[CustomEditor (typeof(Rigidbody2D))] 表示Rigidbody2D這個Component被添加時,會額外執行寫這行的這個Script的內容
通常寫這行的這個Script會放在Editor資料夾,並且繼承 Editor 這個Class,他就能生效了
執行入口通常是 void OnEnable(){ ... } , 目標Component是 內定義變數 target
DisallowMultipleComponent
禁止該Component重複被添加到GameObject上
----------------------------
針對Method使用的Attribue
ContextMenu
可位於Componet名字上按右鍵,執行此Method
[ContextMenu("My Method")] 右鍵點擊Component 會出現 My Method 選項,點擊後可執行此Attribute的Method
MenuItem
為UnityEditor 添加 橫Bar工具列項目
[MenuItem("Assets/Create/New ADVContent",false,0)] 在工具列的Assets -> Create 裡添加項目 New ADVContent,0 = 排行位置最前面
對著專案目錄按右鍵其實就是顯示Assets選單,因此同樣也可看到Create 裡添加項目 New ADVContent
---------------------------
針對變數使用的Attribue
Header
[Header("魔法值")] 顯示一個大標題於 Inspector 上
HideInInspector
不顯示此public 變數於 Inspector上
Multiline
String 變數使用,可多行顯示
Range
[Range(0,100)] 限制變數的輸入範圍
Space
[Space(10)] Inspector 空出10行 pixel
TextArea
string使用,多行顯示
Tooltip
[Tooltip("你好")] 滑鼠移到 Inspector上的該變數時,會顯示提示 "你好"
UnityEngine.Serialization.FormerlySerializedAs
使用於改變數名字,而不丟失在Inspector中已賦予該變數的值
ex: 舊變數名字myVar , 想要把變數名字改為my5566但又不想丟失已給的myVar值
[FormerlySerializedAs("myVar")]
public float my5566