Documentation

Unity Engine

Unity Engine

Unity-specific C# attributes

Understand how to use Unity-specific C# attributes to change how the Unity Editor and runtime treat your code.
Read time 1 minuteLast updated 2 days ago

Attributes in C# are metadata markers that can be placed above a class, field, or method declaration to indicate special behavior.
There are many attributes defined in the .NET libraries and Unity also provides many Unity-specific attributes. For example, you can add the
HideInInspector
attribute above a field declaration to prevent the
Inspector

?

window from showing the field, even if it's public. Place attributes in square brackets above the declaration as follows:
[HideInInspector]public float strength;
For the full list of
UnityEngine
attributes, refer to the list under UnityEngine > Attributes in the Scripting API reference, which begins with
AddComponentMenu
.
For the full list of
UnityEditor
attributes, refer to the list under UnityEditor > Attributes in the Scripting API reference, which begins with
AssetPostprocessorStaticVariableIgnoreAttribute
.
Note
You can use the .NET
ThreadStatic
attribute in Unity scripts. However,
Resources.UnloadUnusedAssets
doesn't always treat a
[ThreadStatic]
field as a reference to an asset, so it might unload an asset that only a
[ThreadStatic]
field references. To keep an asset loaded, also reference it from a regular field.

Additional resources