Unity Learn home
View Tutorial Content
Steps

Attributes

Tutorial
Intermediate
+10 XP
5 Mins
(72)
Summary
Attributes are a way of tagging or labeling pieces of code. By the end to this tutorial, you’ll be able to mark your code with attributes using appropriate syntax
Select your Unity version
Last updated: November 01, 2022
2021.3
2019.3
2019.2
2019.1
2018.4
2018.3
2018.2
2018.1
2017.4
2017.3
2017.2
2017.1
5.x
4.x
Language
English
Also included in

1.Overview

Attributes are a way of tagging or labeling pieces of code. You can then check your code for attributes using a technique called reflection and run code based on what you find.
By the end to this tutorial, you’ll be able to mark your code with attributes using appropriate syntax
Note: Reflection is beyond the scope of this introductory tutorial.

2.Before you begin

This tutorial is part of Beginner Scripting, a collection of basic introductions to a wide range of different programming concepts. If you want a more guided learning experience, try the Create with Code course or the full Junior Programmer pathway.

3.Attribute syntax

Attributes must be surrounded by square brackets. You can write them either ttributes before or above pieces of code. You can add attributes to many different pieces of code, including fields, classes and methods. You can also include parameters that affect how attributes behave.
Here are some examples of attributes added to code.

Field with an attribute

[Range(0f, 10f)] public float speed = 5f;

Class with an attribute

[CustomEditor(typeof(PlayerScript))] public class PlayerScriptEditor : Editor { //… }

Method with an attribute

[InitializeOnLoadMethod] static void Initialization () { // … }
In the rest of this tutorial, you’ll explore the examples above in more detail.

4.The Range attribute

The Range attribute can be applied to integer and float fields which appear in the inspector. When number fields (either floats or integers) are given this attribute their entry in the inspector is replaced by a slider. The attribute takes two parameters: the minimum and maximum values the slider will go between.
[Range(0f, 10f)] public float speed = 5f;

without attributes the default input field is shown
Select image to expand
Default variable field

A slider replaces the input
Select image to expand
How the use of the "Range" attribute changes the editor

5.The CustomEditor attribute

You can use the CustomEditor attribute to match a Unity Object to an editor script that you have created. When you give a class definition (one that inherits from the Editor class) this attribute, its code will be used to display its target in the Inspector.
The CustomEditor attribute takes one parameter: the type of the target for which the attributed class is the editor. Normally this takes the form of a typeof expression.
In the example below, the PlayerScriptEditor will target instances of PlayerScript:
[CustomEditor(typeof(PlayerScript))] public class PlayerScriptEditor : Editor { //… }

6.The InitializeOnLoadMethod attribute

You can use the InitializeOnLoadMethod attribute to mark a static method so that it is called when the Unity Editor is first opened. Creators typically use this attribute to set up editor tools.
In the example below, the Initialization method will be called when the Unity Editor is first opened:
[InitializeOnLoadMethod] static void Initialization () { // … }

7.Summary

In this tutorial, you learned the basics of attributes in C# scripting, including the appropriate syntax for marking pieces of code with an attribute. You also reviewed three common attributes: Range, CustomEditor and InitializeOnLoadMethod.
As you continue your journey as a creator, attributes will become more relevant and will help you to work as efficiently as possible.

Attributes
Attributes
General Tutorial Discussion
0
0
1. Overview
0
0
2. Before you begin
0
0
3. Attribute syntax
0
0
4. The Range attribute
0
0
5. The CustomEditor attribute
0
0
6. The InitializeOnLoadMethod attribute
0
0
7. Summary
0
0