Tutorial
·
intermediate
·
+10XP
·
5 mins
·
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
1. OverviewAttributes 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.
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
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop
Class with an attribute
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop
Method with an attribute
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop
In the rest of this tutorial, you’ll explore the examples above in more detail.
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.
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop
Default variable field
How the use of the "Range" attribute changes the editor
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:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop
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:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4