C# attributes and reflection
From this point forward in the chapter, all editor extensions will rely heavily on the concepts of attributes and reflection. These concepts are not specific to Unity but refer to more general ideas in computer science, programming, and to their application in languages such as C# as well as in the .NET framework. Before proceeding to the next editor extension, let's consider attributes and the related concept of reflection using the example of the Range
attribute, which is native to Unity. Consider the following line of code:
public float MyNumber = 0;
This public variable will be displayed in the Object Inspector with an edit field that allows the user to type in any valid floating point number thereby setting the value of MyNumber
, as shown in the following screenshot:
This code works fine and is suitable in many cases, but there are times when it's preferable to validate the numerical entry to within...