Awesome
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 December 9, 2020:
- HelpURL
Added May 6, 2020:
- GradientUsage
- Min
- InspectorName
- Delayed
- ContextMenuItem
- ImageEffectTransformsToLDR
- ImageEffectAllowedInSceneView
- ImageEffectOpaque
- ImageEffectAfterScale
- ImageEffectUsesCommandBuffer
- AssemblyIsEditorAssembly
- ExcludeFromPreset
- ExcludeFromObjectFactory
- ExcludeFromCoverage
- SharedBetweenAnimatorsAttribute
- BeforeRenderOrderAttribute
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;
Min: Limit minimum value of float or int.
[Min(1.0f)] public float speed = 2.0;
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;
GradientUsage: Use on a gradient to configure the GradientField.
[GradientUsage(true)] public Gradient gradient;
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;
InspectorName: Use on enum to change it's display name.
public enum ModelImporterIndexFormat
{
Auto = 0,
[InspectorName("16 bits")]
UInt16 = 1,
[InspectorName("32 bits")]
UInt32 = 2,
}
Delayed: Use on float, int, or string to delay property being changed until user presses enter or focus changes.
[Delayed] public string health = 100;
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 ResetScore()
{
score = 100;
}
ContextMenuItem: Calls a method for a field.
[ContextMenuItem("Reset Score", "ResetScore")]
var score = 10;
void ResetScore()
{
score = 0;
}
SelectionBase: Will select this GameObject when a sub object is selected in the editor.
[SelectionBase]
public class MyClass : MonoBehaviour
{
}
HelpURL: Will set a custom documentation URL for this component accesible via the help icon from the component view in the Inspector window within the Unity Editor.
[HelpURL("https://example.com/docs/MyComponent")]
public class MyComponent : 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;
}
Image Effect
ImageEffectTransformsToLDR: Use on image effect to cause destination buffer to be LDR buffer.
ImageEffectAfterScale: "Any Image Effect with this attribute will be rendered after Dynamic Resolution stage."
ImageEffectAllowedInSceneView: Allows image effect to work on scene view camera.
[ExecuteInEditMode, ImageEffectAllowedInSceneView]
public class BloomEffect : MonoBehaviour
{
}
ImageEffectOpaque: "Any Image Effect with this attribute will be rendered after opaque geometry but before transparent geometry."
[ImageEffectOpaque]
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
}
ImageEffectUsesCommandBuffer: "Use this attribute when image effects are implemented using Command Buffers."
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
}
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
{
}
BeforeRenderOrder: "Use this BeforeRenderOrderAttribute when you need to specify a custom callback order for Application.onBeforeRender."
SharedBetweenAnimators: "...an attribute that specify that this StateMachineBehaviour should be instantiate only once and shared among all Animator instance. This attribute reduce the memory footprint for each controller instance."
[SharedBetweenAnimatorsAttribute]
public class AttackBehavior : StateMachineBehaviour
{
}
AssemblyIsEditorAssembly: Can use this rather than place the script in an "Editor" folder.
[assembly:AssemblyIsEditorAssembly]
public class MyEditorClass : MonoBehaviour
{
}
ExcludeFromPreset: "Add this attribute to a class to prevent creating a Preset from the instances of the class."
[ExcludeFromPreset]
public class MyClass : MonoBehaviour
{
public int score = 10; // Won't be used in Preset?
}
ExcludeFromObjectFactory: "Add this attribute to a class to prevent the class and its inherited classes from being created with ObjectFactory methods."
ExcludeFromCoverage: "Allows you to exclude an Assembly, Class, Constructor, Method or Struct from Coverage."
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()
{
}