Attributes allow you to attach information to a method, variable, or a class, as you declare it. This is useful for taking existing code and enhancing it, or changing it in some way. Attributes themselves vary greatly in their effects and use. Here we have a Unity Scene with an orb in it. The orb has a "Spin Script" attached to it. Notice the Speed variable of the script, which is currently set to 0. In the Spin Script, we can see that we first declared the integer speed variable and set it to 0. Then, in the Update() method, we rotate the object about the y-axis, based on the current speed. In this way, we could give the Speed variable any value we wanted, and the object's rotating speed would change as a result. This works fine, but what if we wanted to limit the value of the Speed variable? We could write code to detect the current value of the variable and prevent it from going out of range, or we could attach an attribute to it. Attributes are written either directly above, or directly before, the code that they are modifying, and generally don't affect any other parts of the script. In this case, we will be using the Range attribute. The syntax for all attributes starts with an open square bracket. Then, for the Range attribute, we write the keyword "Range", followed by parentheses. Inside the parentheses, we write our minimum and maximum values. We finish the attribute with a closed square bracket. Note in this case, we have put the attribute above the variable declaration. We could've easily put it just before, as well. If we go back into Unity, we can see that we are no longer presented with the same Speed property in the Inspector view. We now have a slider, which goes from our minimum to our maximum values. Running our Scene, we can see that moving the slider directly changes the speed of the variable, which in turn, changes the rotation of the object. Another attribute we will look at is the ExecuteInEditMode attribute. This attribute causes the script that it is attached to to run, even when the Scene is not in Play mode. Here we have another Scene with an orb in it. This orb has a script called "Color Script" attached to it. In the Color Script, we set the color of the object's shared material to red. If we wanted to see this change in Unity without running our Scene, we could apply the ExecuteInEditMode attribute to it. To do this, we open a square bracket either above or before our class name. We put it before the class name because the ExecuteInEditMode attribute is applied to all of the code in a script, not just one part. We then write the keyword, "ExecuteInEditMode", followed by a closing square bracket. This attribute does not have any parameters, and therefore, does not need any parentheses. Back in Unity, we can see that our object has turned red. Our script was executed, even though we never ran our Scene. It is worth noting that you need to be careful with the ExecuteInEditMode attribute. Normally, scripts are run when the Scene is run. When you stop running a Scene, any changes made to the GameObject in a Scene are undone. Scripts that are executed in Edit mode, however, will be able to modify, create, and delete objects in your Scene. Since the changes are not in Play mode, it will not revert, and will instead be permanent. For instance, the orb in the Scene has been modified to actually be red permanently. If we drag another orb into our Scene, it will also be red, since the orb asset was changed. To fix this, we would have to manually change our color back. You can find more information on the ExecuteInEditMode attribute in the documentation linked below.
Attributes allow you to attach information to a method, variable, or a class, as you declare it. This is useful for taking existing code and enhancing it, or changing it in some way. Attributes themselves vary greatly in their effects and use. Here we have a Unity Scene with an orb in it. The orb has a "Spin Script" attached to it. Notice the Speed variable of the script, which is currently set to 0. In the Spin Script, we can see that we first declared the integer speed variable and set it to 0. Then, in the Update() method, we rotate the object about the y-axis, based on the current speed. In this way, we could give the Speed variable any value we wanted, and the object's rotating speed would change as a result. This works fine, but what if we wanted to limit the value of the Speed variable? We could write code to detect the current value of the variable and prevent it from going out of range, or we could attach an attribute to it. Attributes are written either directly above, or directly before, the code that they are modifying, and generally don't affect any other parts of the script. In this case, we will be using the Range attribute. The syntax for all attributes starts with an open square bracket. Then, for the Range attribute, we write the keyword "Range", followed by parentheses. Inside the parentheses, we write our minimum and maximum values. We finish the attribute with a closed square bracket. Note in this case, we have put the attribute above the variable declaration. We could've easily put it just before, as well. If we go back into Unity, we can see that we are no longer presented with the same Speed property in the Inspector view. We now have a slider, which goes from our minimum to our maximum values. Running our Scene, we can see that moving the slider directly changes the speed of the variable, which in turn, changes the rotation of the object. Another attribute we will look at is the ExecuteInEditMode attribute. This attribute causes the script that it is attached to to run, even when the Scene is not in Play mode. Here we have another Scene with an orb in it. This orb has a script called "Color Script" attached to it. In the Color Script, we set the color of the object's shared material to red. If we wanted to see this change in Unity without running our Scene, we could apply the ExecuteInEditMode attribute to it. To do this, we open a square bracket either above or before our class name. We put it before the class name because the ExecuteInEditMode attribute is applied to all of the code in a script, not just one part. We then write the keyword, "ExecuteInEditMode", followed by a closing square bracket. This attribute does not have any parameters, and therefore, does not need any parentheses. Back in Unity, we can see that our object has turned red. Our script was executed, even though we never ran our Scene. It is worth noting that you need to be careful with the ExecuteInEditMode attribute. Normally, scripts are run when the Scene is run. When you stop running a Scene, any changes made to the GameObject in a Scene are undone. Scripts that are executed in Edit mode, however, will be able to modify, create, and delete objects in your Scene. Since the changes are not in Play mode, it will not revert, and will instead be permanent. For instance, the orb in the Scene has been modified to actually be red permanently. If we drag another orb into our Scene, it will also be red, since the orb asset was changed. To fix this, we would have to manually change our color back. You can find more information on the ExecuteInEditMode attribute in the documentation linked below.
I wish this video went in more details with other attributes
Please, more attributes!!!
executeinplaymode is literally a nuke weapon if you want to troll someone
keep em coming
Very helpful thank you
Thanks!
Awesome
[Space(int)]