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()
{
}