http://www.tallior.com/unity-attributes/
The post was updated with attributes from Unity 5.3 RC1.
Unity uses attributes all over the place for declaring different things to the engine.
The latest release (5.3) has a whopping count of 77 (!) attributes to decorate your classes, methods and fields. New attributes added in latest versions are highlighted throughout this post.
This post is a only a summary of all of Unity’s (public) attributes (i skipped over a few related to Boo, or attributes for UNet which deserve a dedicated post of their own). I believe that by this way of exploring APIs, we can learn more and expand our knowledge of what’s available in the engine; I certainly found a few attributes that were useful for my day-to-day job this way !
While this post is only a summary, I’ll attempt to link each attribute to a follow-up post where i demonstrate how, and more importantly – when you should use it.
NOTE: some of these attributes are undocumented and some are probably ‘leftovers’ from older versions that are not used any more.
Leave a comment with how many attributes you actually knew and used <img class="emoji" alt="
(Unity 5) Editor Related
- UnityAPICompatibilityVersionAttribute – Placed at the assembly level, and signals to Unity that the assembly is compatible with a given API version (this will avoid the attempt to “upgrade” the code in the assembly to Unity 5 level).
- (NEW) ColorUsageAttribute – placed on Color fields to determine how the color picker will behave for it.
Inspector Related
- (NEW) HelpURLAttribute – provides a custom URL for documentation.
- PropertyAttribute – A base class to derive custom property attributes from. These attributes are used over script public variables to control how they are drawn in the inspector. For more info see link, link
- TooltipAttribute – A built-in PropertyAttribute that adds a tooltip to script variables in the inspector.
- SpaceAttribute – A built-in PropertyAttribute that adds spacing around script variables in the inspector.
- HeaderAttribute – A built-in PropetyAttribute that adds a header over a script variable in the inspector.
- RangeAttribute – A built-in PropertyAttribute that constrains an int/float field in the inspector to a specified range.
- MultilineAttribute – A built-in PropertyAttribute that changes string editing in the inspector to a multi-line textbox.
- TextAreaAttribute – A built-in PropertyAttribute that allows string editing in the inspector inside a flexible text area.
- HideInInspector – Hides a field from being displayed in the inspector (although it is still serialized).
- SerializeField – Notifies Unity that the field decorated by the attribute should be serialized.
- FormerlySerializedAsAttribute – Provides a way to rename fields after they’ve been assigned without losing their values.
Menu Related
- AddComponentMenu – Allows placing custom scripts (MonoBehaviours) under a different category of the Add Component menu (the default is under Scripts).
- ContextMenu – Add custom commands to the inspector’s context menu of the current script.
- ContextMenuItemAttribute – Used to add a context menu (right-click menu) in the inspector for a specific field. See this link for more information.
- (NEW) CreateAssetMenuAttribute – Marks a ScriptableObject derived class so it will be listed under Assets/Create (this helps in the creation of ScriptableObject instances, instead of having to write your own custom code for instantiating them).
Runtime
- (Unity 5) RuntimeInitializeOnLoadMethodAttribute – Same as the editor counterpart, allows automatically executing a method (when the game runtime loads).
- (Unity 5) SharedBetweenAnimatorsAttribute – Marks a StateMachineBehaviour to be instantiated only once, since it should be shared between animators.
- RPC – Marks a method so that it can be used along with Unity networking and be invoked by remote clients.
- ExecuteInEditMode – Placed over a script (MonoBehaviour) to have it execute even when not in play mode.
- (NEW) PreserveAttribute – prevents a class, method or field from being stripped, in case stripping is enabled
Components Related
- DisallowMultipleComponent – Prevents the same component from being added twice to the same game object.
- RequireComponent – Specifies that a component marked by this attribute requires another component (which will automatically be added when using the component).
- MonoPInvokeCallbackAttribute – Allows registering a static method so it can be passed and invoked from unmanaged code. See this link for more info.
- ThreadSafeAttribute – No documentation