├── AttrExplorer.cs └── README.md /AttrExplorer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using System; 5 | using System.Reflection; 6 | 7 | // If you toggle the toggle_me it will print a bunch of attributes from the UnityEngine and UnityEditor assembly. 8 | // You may find something new and interesting, including stuff that isn't documented anywhere. 9 | 10 | [ExecuteInEditMode] 11 | public class AttrExplorer : MonoBehaviour 12 | { 13 | public bool toggle_me = false; 14 | 15 | void OnValidate() 16 | { 17 | System.Reflection.Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies(); 18 | foreach (var assembly in assemblies) { 19 | // Only show attributes for UnityEngine and UnityEditor. 20 | if (assembly.FullName.StartsWith("UnityEngine") || 21 | assembly.FullName.StartsWith("UnityEditor")) { 22 | Type[] types = assembly.GetTypes(); 23 | for (var i = 0; i < types.Length; i++) { 24 | // Check if it is an attribute. 25 | if (types[i].IsSubclassOf(typeof(Attribute))) 26 | print(types[i]); 27 | } 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity-Built-In-Attributes 2 | A list of built in Unity Attributes. 3 | * [Property Inspector](#property-inspector) 4 | * [Component Related](#component-related) 5 | * [Serialization](#serialization) 6 | * [Other](#other) 7 | * [Undocumented](#undocumented) 8 | * [Editor](#editor) 9 | 10 | Note: Attributes can be placed in a single set of square brackets: 11 | ```c# 12 | [HideInInspector][SerializeField] int score; 13 | // can be 14 | [HideInInspector, SerializeField] int score; 15 | ``` 16 | 17 | These aren't all the attributes available, and a few of them are system attributes, not Unity ones. 18 | 19 | Added December 9, 2020: 20 | - HelpURL 21 | 22 | Added May 6, 2020: 23 | - GradientUsage 24 | - Min 25 | - InspectorName 26 | - Delayed 27 | - ContextMenuItem 28 | - ImageEffectTransformsToLDR 29 | - ImageEffectAllowedInSceneView 30 | - ImageEffectOpaque 31 | - ImageEffectAfterScale 32 | - ImageEffectUsesCommandBuffer 33 | - AssemblyIsEditorAssembly 34 | - ExcludeFromPreset 35 | - ExcludeFromObjectFactory 36 | - ExcludeFromCoverage 37 | - SharedBetweenAnimatorsAttribute 38 | - BeforeRenderOrderAttribute 39 | 40 | Added July 3, 2018: 41 | - DidReloadScripts 42 | - PostProcessScene 43 | - PostProcessBuild 44 | - Preserve 45 | - RejectDragAndDropMaterial 46 | - CustomGridBrush 47 | - IconName 48 | 49 | # Property Inspector 50 | [HideInInspector](https://docs.unity3d.com/ScriptReference/HideInInspector.html): Stops the property from showing up in the inspector. 51 | ```c# 52 | [HideInInspector] public bool reset = false; 53 | ``` 54 | 55 | [Range](https://docs.unity3d.com/ScriptReference/RangeAttribute.html): Limit the range of a float or int. 56 | ```c# 57 | [Range(0, 100)] public float speed = 2f; 58 | ``` 59 | 60 | [Min](https://docs.unity3d.com/ScriptReference/MinAttribute.html): Limit minimum value of float or int. 61 | ```c# 62 | [Min(1.0f)] public float speed = 2.0; 63 | ``` 64 | 65 | [Multiline](https://docs.unity3d.com/ScriptReference/MultilineAttribute.html): Show more than one lines. 66 | ```c# 67 | [Multiline(4)] public string description = ""; 68 | ``` 69 | 70 | [TextArea](https://docs.unity3d.com/ScriptReference/TextAreaAttribute.html): Draw a flexible scrollable text area. 71 | ```c# 72 | [TextArea] public string description = ""; 73 | ``` 74 | 75 | [ColorUsage](https://docs.unity3d.com/ScriptReference/ColorUsageAttribute.html): Allow alpha channel to be modified, and allow HDR mode. 76 | ```c# 77 | [ColorUsage(true, true)] public Color color = Color.white; 78 | ``` 79 | 80 | [GradientUsage](https://docs.unity3d.com/ScriptReference/GradientUsageAttribute.html): Use on a gradient to configure the GradientField. 81 | ```c# 82 | [GradientUsage(true)] public Gradient gradient; 83 | ``` 84 | 85 | [Space](https://docs.unity3d.com/ScriptReference/SpaceAttribute.html): Add space between inspector elements. 86 | ```c# 87 | public float item1 = 0f; 88 | [Space(10)] 89 | public float item2 = 0f; 90 | ``` 91 | 92 | [Header](https://docs.unity3d.com/ScriptReference/HeaderAttribute.html): Shows a bold label in the inspector. 93 | ```c# 94 | [Header("Stats")] 95 | public int health = 100; 96 | public float speed = 0f; 97 | [Header("Items")] 98 | public int ammo = 10; 99 | ``` 100 | 101 | [Tooltip](https://docs.unity3d.com/ScriptReference/TooltipAttribute.html): Text shown on mouse over. 102 | ```c# 103 | [Tooltip("The games score.")] public int score = 0; 104 | ``` 105 | 106 | [InspectorName](https://docs.unity3d.com/ScriptReference/InspectorNameAttribute.html): Use on enum to change it's display name. 107 | ```c# 108 | public enum ModelImporterIndexFormat 109 | { 110 | Auto = 0, 111 | [InspectorName("16 bits")] 112 | UInt16 = 1, 113 | [InspectorName("32 bits")] 114 | UInt32 = 2, 115 | } 116 | ``` 117 | 118 | [Delayed](https://docs.unity3d.com/ScriptReference/DelayedAttribute.html): Use on float, int, or string to delay property being changed until user presses enter or focus changes. 119 | ```c# 120 | [Delayed] public string health = 100; 121 | ``` 122 | 123 | # Component Related 124 | [DisallowMultipleComponent](https://docs.unity3d.com/ScriptReference/DisallowMultipleComponent.html): Prevent more than 1 of this component being on a GameObject. 125 | ```c# 126 | [DisallowMultipleComponent] 127 | public class MyScript : MonoBehaviour 128 | { 129 | } 130 | ``` 131 | 132 | [RequireComponent](https://docs.unity3d.com/ScriptReference/RequireComponent.html): Tell GameObject to add this component if it isn't already added. 133 | ```c# 134 | [RequireComponent(typeof(RigidBody))] 135 | [RequireComponent(typeof(Component1), typeof(Component2), typeof(Component3))] // You can enter multiple components into attribute. 136 | public class MyClass : MonoBehaviour 137 | { 138 | } 139 | ``` 140 | 141 | [ExecuteInEditMode](https://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html): Will call MonoBehaviour methods like Update and OnEnable while in EditMode. 142 | ```c# 143 | [ExecuteInEditMode] 144 | public class MyClass : MonoBehaviour 145 | { 146 | } 147 | ``` 148 | 149 | [ContextMenu](https://docs.unity3d.com/ScriptReference/ContextMenu.html): Add a context menu to a MonoBehaviour or ScriptableObject. 150 | ```c# 151 | [ContextMenu("Reset Score")] 152 | public void ResetScore() 153 | { 154 | score = 100; 155 | } 156 | ``` 157 | 158 | [ContextMenuItem](https://docs.unity3d.com/ScriptReference/ContextMenuItemAttribute.html): Calls a method for a field. 159 | ```c# 160 | [ContextMenuItem("Reset Score", "ResetScore")] 161 | var score = 10; 162 | 163 | void ResetScore() 164 | { 165 | score = 0; 166 | } 167 | ``` 168 | 169 | [SelectionBase](https://docs.unity3d.com/ScriptReference/SelectionBaseAttribute.html): Will select this GameObject when a sub object is selected in the editor. 170 | ```c# 171 | [SelectionBase] 172 | public class MyClass : MonoBehaviour 173 | { 174 | } 175 | ``` 176 | 177 | [HelpURL](https://docs.unity3d.com/ScriptReference/HelpURLAttribute.html): 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. 178 | ```c# 179 | [HelpURL("https://example.com/docs/MyComponent")] 180 | public class MyComponent : MonoBehaviour 181 | { 182 | } 183 | ``` 184 | 185 | # Serialization 186 | [SerializeField](https://docs.unity3d.com/ScriptReference/SerializeField.html): Force Unity to serialize a private field. 187 | ```c# 188 | [SerializeField] private int score; 189 | ``` 190 | 191 | [NonSerialized](https://docs.unity3d.com/ScriptReference/NonSerializable.html): Prevent Unity from serializing a public field. 192 | ```c# 193 | [NonSerialized] public int score; 194 | ``` 195 | 196 | [FormerlySerializedAs](https://docs.unity3d.com/ScriptReference/Serialization.FormerlySerializedAsAttribute.html): If you changed the name of a serialized property, you can set this to the old name, so save data will still work. 197 | ```c# 198 | [FormerlySerializedAs("myValue")] private string m_MyValue; 199 | ``` 200 | 201 | [Serializable](https://docs.unity3d.com/ScriptReference/Serializable.html): Make a class Serializable so it will be visible in the inspector. 202 | ```c# 203 | [System.Serializable] 204 | public class MyClass 205 | { 206 | public int myInt = 10; 207 | public Color myColor = Color.white; 208 | } 209 | ``` 210 | 211 | # Image Effect 212 | [ImageEffectTransformsToLDR](https://docs.unity3d.com/ScriptReference/ImageEffectTransformsToLDR.html): Use on image effect to cause destination buffer to be LDR buffer. 213 | 214 | [ImageEffectAfterScale](https://docs.unity3d.com/ScriptReference/ImageEffectAfterScale.html): "Any Image Effect with this attribute will be rendered after Dynamic Resolution stage." 215 | 216 | [ImageEffectAllowedInSceneView](https://docs.unity3d.com/ScriptReference/ImageEffectAllowedInSceneView.html): Allows image effect to work on scene view camera. 217 | ```c# 218 | [ExecuteInEditMode, ImageEffectAllowedInSceneView] 219 | public class BloomEffect : MonoBehaviour 220 | { 221 | } 222 | ``` 223 | 224 | [ImageEffectOpaque](https://docs.unity3d.com/ScriptReference/ImageEffectOpaque.html): "Any Image Effect with this attribute will be rendered after opaque geometry but before transparent geometry." 225 | ```c# 226 | [ImageEffectOpaque] 227 | void OnRenderImage(RenderTexture source, RenderTexture destination) 228 | { 229 | } 230 | ``` 231 | 232 | [ImageEffectUsesCommandBuffer](https://docs.unity3d.com/ScriptReference/ImageEffectUsesCommandBuffer.html): "Use this attribute when image effects are implemented using Command Buffers." 233 | ```c# 234 | void OnRenderImage(RenderTexture source, RenderTexture destination) 235 | { 236 | } 237 | ``` 238 | 239 | # Other 240 | [RuntimeInitializeOnLoadMethod](https://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoadMethodAttribute.html): Calls a method once before or after the first scene has loaded. Good for initializing Singletons without having to place objects in the scene. 241 | ```c# 242 | [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] 243 | static void OnLoad() 244 | { 245 | Debug.Log("Create Singletons"); 246 | } 247 | ``` 248 | 249 | [CreateAssetMenu](https://docs.unity3d.com/ScriptReference/CreateAssetMenuAttribute.html): Add an option to Assets/Create for creating a ScriptableObject. 250 | ```c# 251 | [CreateAssetMenu(menuName = "My ScriptableObject", order = 100)] 252 | public class MyScriptableObject : ScriptableObject 253 | { 254 | } 255 | ``` 256 | 257 | [Preserve](https://docs.unity3d.com/ScriptReference/Scripting.PreserveAttribute.html): Preserves a members name when converting to bytecode. Useful if referencing members through reflection. 258 | ```c# 259 | [Preserve] 260 | static void Boink() 261 | { 262 | Debug.Log("Boink"); 263 | } 264 | ``` 265 | 266 | [CustomGridBrush](https://docs.unity3d.com/ScriptReference/CustomGridBrushAttribute.html): "define the class as a grid brush and to make it available in the palette window." 267 | ```c# 268 | [CustomGridBrush(true, true, true, "Default Brush")] 269 | public class MyBrush : GridBrush 270 | { 271 | } 272 | ``` 273 | 274 | [BeforeRenderOrder](https://docs.unity3d.com/ScriptReference/BeforeRenderOrderAttribute.html): "Use this BeforeRenderOrderAttribute when you need to specify a custom callback order for Application.onBeforeRender." 275 | 276 | [SharedBetweenAnimators](https://docs.unity3d.com/ScriptReference/SharedBetweenAnimatorsAttribute.html): "...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." 277 | ```c# 278 | [SharedBetweenAnimatorsAttribute] 279 | public class AttackBehavior : StateMachineBehaviour 280 | { 281 | } 282 | ``` 283 | 284 | [AssemblyIsEditorAssembly](https://docs.unity3d.com/ScriptReference/AssemblyIsEditorAssembly.html): Can use this rather than place the script in an "Editor" folder. 285 | ```c# 286 | [assembly:AssemblyIsEditorAssembly] 287 | 288 | public class MyEditorClass : MonoBehaviour 289 | { 290 | } 291 | ``` 292 | 293 | [ExcludeFromPreset](https://docs.unity3d.com/ScriptReference/ExcludeFromPresetAttribute.html): "Add this attribute to a class to prevent creating a Preset from the instances of the class." 294 | ```c# 295 | [ExcludeFromPreset] 296 | public class MyClass : MonoBehaviour 297 | { 298 | public int score = 10; // Won't be used in Preset? 299 | } 300 | ``` 301 | 302 | [ExcludeFromObjectFactory](https://docs.unity3d.com/ScriptReference/ExcludeFromObjectFactoryAttribute.html): "Add this attribute to a class to prevent the class and its inherited classes from being created with ObjectFactory methods." 303 | 304 | [ExcludeFromCoverage](https://docs.unity3d.com/ScriptReference/TestTools.ExcludeFromCoverageAttribute.html): "Allows you to exclude an Assembly, Class, Constructor, Method or Struct from Coverage." 305 | 306 | # Undocumented 307 | DefaultExecutionOrder: Probably sets the Script Execution order. 308 | ```c# 309 | [DefaultExecutionOrder(100)] 310 | public class MyScript : MonoBehaviour 311 | { 312 | } 313 | ``` 314 | 315 | RejectDragAndDropMaterial: Probably prevents materials being applied through dragging and dropping in the editor. 316 | ```c# 317 | [RejectDragAndDropMaterial] 318 | public class MyRenderer : MonoBehaviour 319 | { 320 | } 321 | ``` 322 | 323 | UnityEditor.IconName: Probably allows you to set a scripts icon? 324 | 325 | 326 | # Editor 327 | These should be used in scripts that are inside an Editor folder. 328 | 329 | [MenuItem](https://docs.unity3d.com/ScriptReference/MenuItem.html): Adds a menu to the Editor toolbar. 330 | ```c# 331 | [MenuItem("MyMenu/Do Something")] 332 | static void DoSomething() 333 | { 334 | Debug.Log("Doing Something..."); 335 | } 336 | ``` 337 | 338 | [InitializeOnLoadMethod](https://docs.unity3d.com/ScriptReference/InitializeOnLoadMethodAttribute.html): Called after scripts have been compiled. 339 | ```c# 340 | [InitializeOnLoadMethod] 341 | static void OnProjectLoadedInEditor() 342 | { 343 | Debug.Log("Project loaded in Unity Editor"); 344 | } 345 | ``` 346 | 347 | [DidReloadScripts](https://docs.unity3d.com/ScriptReference/Callbacks.DidReloadScripts.html): Called after scripts have reloaded. Can take an order parameter. Methods with lower orders are called earlier. 348 | ```c# 349 | using UnityEditor.Callbacks; 350 | 351 | [DidReloadScripts(100)] 352 | static void OnScriptsReloaded() 353 | { 354 | Debug.Log("Reloaded."); 355 | } 356 | #endif 357 | ``` 358 | 359 | [OnOpenAsset](https://docs.unity3d.com/ScriptReference/Callbacks.OnOpenAssetAttribute.html): Called when double clicking an asset in the project browser. 360 | ```c# 361 | using UnityEditor.Callbacks; 362 | 363 | [OnOpenAssetAttribute(1)] 364 | static bool step1(int instanceID, int line) 365 | { 366 | string name = EditorUtility.InstanceIDToObject(instanceID).name; 367 | Debug.Log("Open Asset step: 1 (" + name + ")"); 368 | return false; // we did not handle the open 369 | } 370 | ``` 371 | 372 | [PostProcessBuild](https://docs.unity3d.com/ScriptReference/Callbacks.PostProcessBuildAttribute.html): Called after game has been built. 373 | ```c# 374 | using UnityEditor.Callbacks; 375 | 376 | [PostProcessBuildAttribute(1)] 377 | public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) 378 | { 379 | } 380 | ``` 381 | 382 | [PostProcessScene](https://docs.unity3d.com/ScriptReference/Callbacks.PostProcessSceneAttribute.html): Called after scene has been built. 383 | ```c# 384 | using UnityEditor.Callbacks; 385 | 386 | [PostProcessSceneAttribute (2)] 387 | public static void OnPostprocessScene() 388 | { 389 | } 390 | ``` 391 | --------------------------------------------------------------------------------