Unity-Built-In-Attributes 內建屬性
Unity-Built-In-Attributes
https://github.com/teebarjunk/Unity-Built-In-Attributes/blob/master/README.md#unity-built-in-attributes
A list of built in Unity Attributes.
Note: Attributes can be placed in a single set of square brackets:
[HideInInspector][SerializeField] int score;
// can be
[HideInInspector, SerializeField] int score;
These aren't all the attributes available, and a few of them are system attributes, not Unity ones.
Added July 3, 2018:
- DidReloadScripts
- PostProcessScene
- PostProcessBuild
- Preserve
- RejectDragAndDropMaterial
- CustomGridBrush
- IconName
Property Inspector
HideInInspector: Stops the property from showing up in the inspector.
[HideInInspector] public bool reset = false;
Range: Limit the range of a float or int.
[Range(0, 100)] public float speed = 2f;
Multiline: Show more than one lines.
[Multiline(4)] public string description = "";
TextArea: Draw a flexible scrollable text area.
[TextArea] public string description = "";
ColorUsage: Allow alpha channel to be modified, and allow HDR mode.
[ColorUsage(true, true)] public Color color = Color.white;
Space: Add space between inspector elements.
public float item1 = 0f;
[Space(10)]
public float item2 = 0f;
Header: Shows a bold label in the inspector.
[Header("Stats")]
public int health = 100;
public float speed = 0f;
[Header("Items")]
public int ammo = 10;
ToolTip: Text shown on mouse over.
[ToolTip("The games score.")] public int score = 0;
Component Related
DisallowMultipleComponent: Prevent more than 1 of this component being on a GameObject.
[DisallowMultipleComponent]
public class MyScript : MonoBehaviour
{
}
RequireComponent: Tell GameObject to add this component if it isn't already added.
[RequireComponent(typeof(RigidBody))]
[RequireComponent(typeof(Component1), typeof(Component2), typeof(Component3))] // You can enter multiple components into attribute.
public class MyClass : MonoBehaviour
{
}
ExecuteInEditMode: Will call MonoBehaviour methods like Update and OnEnable while in EditMode.
[ExecuteInEditMode]
public class MyClass : MonoBehaviour
{
}
ContextMenu: Add a context menu to a MonoBehaviour or ScriptableObject.
[ContextMenu("Reset Score")]
public void ResetHealth()
{
health = 100;
}
SelectionBase: Will select this GameObject when a sub object is selected in the editor.
[SelectionBase]
public class MyClass : MonoBehaviour
{
}
Serialization
SerializeField: Force Unity to serialize a private field.
[SerializeField] private int score;
NonSerialized: Prevent Unity from serializing a public field.
[NonSerialized] public int score;
FormerlySerializedAs: If you changed the name of a serialized property, you can set this to the old name, so save data will still work.
[FormerlySerializedAs("myValue")] private string m_MyValue;
Serializable: Make a class Serializable so it will be visible in the inspector.
[System.Serializable]
public class MyClass
{
public int myInt = 10;
public Color myColor = Color.white;
}
Other
RuntimeInitializeOnLoadMethod: Calls a method once before or after the first scene has loaded. Good for initializing Singletons without having to place objects in the scene.
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void OnLoad()
{
Debug.Log("Create Singletons");
}
CreateAssetMenu: Add an option to Assets/Create for creating a ScriptableObject.
[CreateAssetMenu(menuName = "My ScriptableObject", order = 100)]
public class MyScriptableObject : ScriptableObject
{
}
Preserve: Preserves a members name when converting to bytecode. Useful if referencing members through reflection.
[Preserve]
static void Boink()
{
Debug.Log("Boink");
}
CustomGridBrush: "define the class as a grid brush and to make it available in the palette window."
[CustomGridBrush(true, true, true, "Default Brush")]
public class MyBrush : GridBrush
{
}
Undocumented
DefaultExecutionOrder: Probably sets the Script Execution order.
[DefaultExecutionOrder(100)]
public class MyScript : MonoBehaviour
{
}
RejectDragAndDropMaterial: Probably prevents materials being applied through dragging and dropping in the editor.
[RejectDragAndDropMaterial]
public class MyRenderer : MonoBehaviour
{
}
UnityEditor.IconName: Probably allows you to set a scripts icon?
Editor
These should be used in scripts that are inside an Editor folder.
MenuItem: Adds a menu to the Editor toolbar.
[MenuItem("MyMenu/Do Something")]
static void DoSomething()
{
Debug.Log("Doing Something...");
}
InitializeOnLoadMethod: Called after scripts have been compiled.
[InitializeOnLoadMethod]
static void OnProjectLoadedInEditor()
{
Debug.Log("Project loaded in Unity Editor");
}
DidReloadScripts: Called after scripts have reloaded. Can take an order parameter. Methods with lower orders are called earlier.
using UnityEditor.Callbacks;
[DidReloadScripts(100)]
static void OnScriptsReloaded()
{
Debug.Log("Reloaded.");
}
#endif
OnOpenAsset: Called when double clicking an asset in the project browser.
using UnityEditor.Callbacks;
[OnOpenAssetAttribute(1)]
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
}
PostProcessBuild: Called after game has been built.
using UnityEditor.Callbacks;
[PostProcessBuildAttribute(1)]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
}
PostProcessScene: Called after scene has been built.
using UnityEditor.Callbacks;
[PostProcessSceneAttribute (2)]
public static void OnPostprocessScene()
{
}
相關文章
- Python:PyHook3的HookManager內建屬性以及相關函式PythonHook函式
- 行內元素屬性設定
- Python __dict__屬性:檢視物件內部所有屬性名和屬性值組成的字典Python物件
- jQuery - 獲取內容和屬性jQuery
- jQuery - 設定內容和屬性jQuery
- MongoDB – 使用模式構建之屬性模式MongoDB模式
- java判斷實體內中屬性值內容是否有變更(包含父類屬性值)Java
- -webkit-overflow-scrolling:touch屬性副作用–QQ內建瀏覽器空白處理WebKit瀏覽器
- JavaScript內建物件屬性及方法大全JavaScript物件
- 行內元素的padding和margin屬性padding
- 物件導向--內部屬性型別物件型別
- CMake 屬性之全域性屬性
- 使用CSS自定義屬性構建骨架屏CSS
- 內嵌標籤frameset框架屬性及用法框架
- defer 屬性和 async 屬性
- js--物件內部屬性與 Object.defineProperty()JS物件Object
- CMake 屬性之目錄屬性
- CMake 屬性之目標屬性
- CSS 屬性篇(七):Display屬性CSS
- 前端筆記之JavaScript物件導向(二)內建建構函式&相關方法|屬性|運算子&繼承&物件導向前端筆記JavaScript物件函式繼承
- CSS內聯樣式的使用,設定字型屬性CSS
- jQuery捕獲-獲取DOM元素內容和屬性jQuery
- 010-jQuery獲取和設定內容屬性jQuery
- Python - 物件導向程式設計 - 公共屬性、保護屬性、私有屬性Python物件程式設計
- 根據屬性字串獲取屬性值字串
- CAD屬性編輯操作——物件屬性教程物件
- CSS字型屬性和文字屬性詳解CSS
- Python 類的屬性與例項屬性Python
- python物件屬性管理(2):property管理屬性Python物件
- ref屬性
- HTML 屬性HTML
- DOM屬性
- Property屬性
- onreadystatechange 屬性
- jQuery 屬性jQuery
- background 屬性
- 私有屬性
- background屬性