A Summary of Unity Attributes

本文总结了Unity引擎中可用的所有公共属性,包括编辑器相关、检查器相关、菜单相关、运行时相关及组件相关等属性。这些属性能帮助开发者更好地控制脚本的显示方式和行为,并介绍了一些较少为人知但实用的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.tallior.com/unity-attributes/

The post was updated with attributes from Unity 5.3 RC1.

Unity usesattributes 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 linklink
  • 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

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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值